How to send email in Laravel?
How to send email in Laravel?

In this part of the Laravel tutorial, we will introduce a simple way to send emails. Laravel 8 provides Email Class for sending emails and allows you to use multiple drivers for this purpose. The drivers you can use for sending emails are:

  • SMTP
  • Mailgun
  • Postmark
  • Amazon SES
  • sendmail

In this tutorial, we will use Gmail SMTP to send emails. This allows us to easily send emails on localhost. By following the steps below, you will be able to easily send emails to your users using Laravel.

  • Step 1: Configure the .env file

You must specify which driver you want to use to send your emails; the specification will be done in the .env file of your Laravel project. What you need to specify in this file are the email driver, the email host, the email port, the email username and the email password. Find the .env file of your Laravel project and apply the following changes to it:

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail@gmail.com
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_gmail@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
  • Step 2: Create an email class

Here we will create the MyTestMail class for sending emails. First run the following command in the command prompt to create the email class.

php artisan make:mail MyTestMail

Then go to app> Mail and you will see that the corresponding class has been created and contains the following commands.

app/Mail/MyTestMail.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $details;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from ItSolutionStuff.com')
                    ->view('emails.myTestMail');
    }
}
  • Step 3: Create a Blade View

In this section, we will create a folder called emails in the Laravel project views section and will add a blade view called myTestMail to it. Since our goal is to send a simple email, we do not need a specific style for this view. But you can easily style it.

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>ItsolutionStuff.com</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>Thank you</p>
</body>
</html>

Note: If the message or the body of your email contains html tags instead of {{ $details ['body'] }} use {!! $details ['body'] !!} .

  • Step 4: Add Route

Now it's time to create a route to send our email. We will create the following web route to test the codes.

routes/web.php

Route::get('send-mail', function () {
   
    $details = [
        'title' => 'Mail from ItSolutionStuff.com',
        'body' => 'This is for testing email using smtp'
    ];
   
    \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMail($details));
   
    dd("Email is Sent.");
});

You can now run the project with the following command:

php artisan serve

By going to the following path you will see that the email will be sent correctly:

http://localhost:8000/send-mail

Note that: by default, your Gmail account does not allow access to low-security applications. In order for you to be able to send emails from localhost by using your Gmail, you need to follow these steps: