Learn to send emails using Gmail and Sendgrid in Laravel 5 - BOOKC -->

Learn to send emails using Gmail and Sendgrid in Laravel 5

Wednesday, November 6, 2019

Laravel provides many methods to send emails. You may use a plain PHP method to send emails, or you may use some email service providers such as Sendinblue, Mailgun, Sendgrid, Mandrill, Amazon SES, etc.
To send emails on a production server, simply edit the mail.php configuration file, which is placed in the config directory.
Here is the file without comments:
return [

    'driver' => env('MAIL_DRIVER', 'smtp'),

    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

    'port' => env('MAIL_PORT', 587),

    'from' => ['address' => null, 'name' => null],

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    'username' => env('MAIL_USERNAME'),

    'password' => env('MAIL_PASSWORD'),

    'sendmail' => '/usr/sbin/sendmail -bs',

    'pretend' => false,
To send emails on a local development server (Homestead), simply edit the .env file.
As usual, you may learn how to use Mailgun, Mandrill and SES drivers at the official docs:
Because we're working on Homestead, we will learn how to send emails on Homestead using Gmail and Sendinblue for FREE!!!!!

Sending emails using Gmail

If you have a gmail account, it's very easy to send emails using Laravel 5!
First, go to:
Take a look at the Sign-in & security -> Connected apps & sites -> Allow less secure apps settings.
You must turn the option "Allow less secure apps" ON.
Configure Gmail
Once complete, edit the .env file:
Well done! You're now ready to send emails using Gmail!
If you get this error when sending email: "Failed to authenticate on SMTP server with username "youremail@gmail.com" using 3 possible authenticators"
You may try one of these methods:
  • Go to https://accounts.google.com/UnlockCaptcha, click continue and unlock your account for access through other media/sites.
  • Using a double quote password: "your password"
  • Try to use only your Gmail username: yourGmailUsername

Sending emails using Sendinblue

SendGrid isn't free any more, so we'll use Sendinblue instead.
Go to Sendinblue, register a new account:
When your account is activated, edit the .env file:
Good job! You're now ready to send emails using Sendinblue!

Sending a test email

To send a test email, open routes.php file and add this route:
Route::get('sendemail', function () {

    $data = array(
        'name' => "Learning Laravel",

    Mail::send('emails.welcome', $data, function ($message) {

        $message->from('yourEmail@domain.com', 'Learning Laravel');

        $message->to('yourEmail@domain.com')->subject('Learning Laravel test email');


    return "Your email has been sent successfully";

As you see, we use the send method on the Mail facade. There are three arguments:
  1. The name of the view that we use to send emails.
  2. An array of data that we want to pass to the email.
  3. A closure that we can use to customize our email subjects, sender, recipients, etc.
When you visit http://homestead.app/sendemail, Laravel will try to send an email. If the email is sent successfully, Laravel will display a message.
Note: Be sure to replace yourEmail@domain.com with your real email address
Finally, we don't have the welcome.blade.php view yet, let's create it and put it in the emails directory.
<!DOCTYPE html>
<html lang="en-US">
    <meta charset="utf-8">
<h2>Learning Laravel!</h2>

    Welcome to {!! $name !!} website!

Because we've passed an array containing the $name key in the above route, we could display the name within this welcome view using:
{!! $name !!}
<?php echo $name ?>
Done! Now go to http://homestead.app/sendemail, you should see: