How to Send Mail in Laravel: A Step-by-Step Guide

Sending mail in Laravel is easy. This step-by-step guide will show you how to send emails with examples and code snippets. Learn how to customize the email messages, and configure your SMTP server. There are 3 steps to implement the send mail in Laravel.

  • Configure the SMTP in the .env.
  • Create a mail class for constructing the data and send it to the template.
  • Create a mail template for mail style and text.

Creating mail class

To generate the SimpleMail class type the command below.

php artisan make:mail SimpleMail

The command will create the SimpleMail.php in the directory app/Mail. This class is where you handle data and return the actual mail template. In this mail class you can use construct to accept any dynamic render data that need to be passed down to the template and the build function is where you can return the view with the data passed by the construct. See the sample code below.

In app/Mail/SimpleMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SimpleMail extends Mailable
{
    use Queueable, SerializesModels;
    private $content;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.sample', ['content' => $this->content]);
    }
}

Creating emails template

Go to resources/views create folder emails and sample.blade.php file in that folder. This view is where you will write any HTML that represents the actual view of the mail being sent. You can use the “{{}}” syntax to display the variable data passed by the mail class, in this example $content. 

In resources/views/emails/sample.blade.php

<div>
    <p>{{ $content }}</p>
</div>

Creating a mail controller for sending email

To generate MailController type the below command. This will create the MailController.php in the app/Http/Controllers folder. In this mail controller, we will show you an example of how to send mail to users with the simple mail that we just created above.

php artisan make:controller MailController

In app/Http/Controllers/MailController.php

<?php

namespace App\Http\Controllers;

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

class MailController extends Controller
{
    public function sendSampleMail()
    {
        Mail::to('tomail@gmail.com')->send(new SimpleMail('Sample MAIL'));
        return response()->json(['message' => 'success']);
    }
}

Test send a mail with API route

We will use this API route to call the mail controller to test the send mail implementation.

In routes/api.php

<?php

use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::get('mail', [MailController::class, 'sendSampleMail']);

Config mail SMTP server 

For the below config, we will use the Gmail configuration as the sample. This mail will send from our own Gmail account.

In .env file

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=fromgmail@gmail.com
MAIL_PASSWORD=fromgmailpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=fromgmail@gmail.com
MAIL_FROM_NAME="TEST MAIL"

If you’re using a Gmail account as a mail server. Make sure to go to your Gmail account and turn on less secure in other to let the application access your Gmail. This will allow the Laravel application to use your Gmail account to send emails.