Sending emails in laravel 10.x

A step by step guide to send emails in Laravel 10.x

Apr 2, 2023

Introduction


In modern web applications, sending emails is an essential feature for user communication, order notifications, and various other purposes. Laravel, a popular PHP framework, provides a convenient way to manage and send emails using a powerful feature called "mailables."

Step 1: Generate a Mailable
To begin, you need to generate a "mailable" class using the make:mail Artisan command. Open your terminal and run the following command:

php artisan make:mail exampleMail

This command will generate a new mailable class named exampleMail inside the app/Mail directory.

Step 2: Configure the Sender (From Address)

In the exampleMail class, you can configure the sender (the "from" address) using the envelope method. You can either specify the "from" address directly within the method or set a global "from" address in the config/mail.php configuration file.

Option 1: Setting the "from" address directly in the envelope method:

use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Envelope;

public function envelope(): Envelope
{
    return new Envelope(
        from:   new Address('cercie@example.com', 'Cercie Way'),
        to:     "testUser@gmailine.com",
        subject:"Example Mail",
    );
}

Option 2: Setting a global "from" address in the config/mail.php file:

// config/mail.php

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'cercie@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

The env function is used to fetch the value of the environment variable named MAIL_FROM_ADDRESS. If the MAIL_FROM_ADDRESS environment variable is not set, it will use the default value 'cercie@example.com'.

Step 3: Configuring the View

In the content method of the exampleMail class, you can specify which Blade template should be used when rendering the email's contents. This is where you define the view for the email's HTML content.

public function content(): Content
{
    return new Content(
        view: 'emails.exampleMail',
    );
}

Make sure you have a corresponding Blade template in your resources/views directory. For this example, create a new folder emails inside the resources/views directory and put your email template inside it. The template file should be named exampleMail.blade.php so the full path would be resources/views/emails/exampleMail.blade.php.

Step 4: Customise the Email Content

In the Blade template exampleMail.blade.php, you can customize the email's content using standard Blade syntax. You have the full power of Blade available to create your email's HTML.

Example   exampleMail.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Example Email</title>
</head>
<body>
    <h1>Hello!</h1>
    <p>Your order has been sent.Thank you for using our services!</p>
</body>
</html>

Step 5: Sending the Email

Now that you have set up the mailable class and the view template, you can send the email from any part of your Laravel application. To send the email, you can use the Mail facade, and call the send method with the ExampleMail mailable instance as an argument.

Example of sending the email:

use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;

// Somewhere in your code where you want to send the email

Mail::send(new ExampleMail($order));

Conclusion


As you continue to explore and develop your Laravel applications, remember that email communication plays a vital role in providing a seamless user experience. By harnessing the power of mailables, you can ensure that your users stay informed, engaged, and delighted with the services your application offers.

With the knowledge gained from this tutorial, you are well-equipped to take your email communication to the next level, enriching your application's functionality and user interactions. Happy coding and happy emailing with Laravel!