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 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.

MAIL_DRIVER=mail
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

As usual, you may learn how to use Mailgun, Mandrill and SES drivers at the official docs:

https://laravel.com/docs/master/mail

Because we're working on Homestead, we will learn how to send emails on Homestead using Gmail and Sendgrid 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:

https://myaccount.google.com/security#connectedapps

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:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourEmail
MAIL_PASSWORD=yourPassword
MAIL_ENCRYPTION=tls

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 "[email protected]" 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 Sendgrid

Go to SendGrid, register a new account:

https://sendgrid.com

When your account is activated, edit the .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=yourSendgridUsername
MAIL_PASSWORD=yourPassword

Good job! You're now ready to send emails using Sendgrid!

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([email protected]', 'Learning Laravel');

        $message->to([email protected]')->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 [email protected] 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">
<head>
    <meta charset="utf-8">
</head>
<body>
<h2>Learning Laravel!</h2>

<div>
    Welcome to {!! $name !!} website!
</div>

</body>
</html>

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 !!}

or

<?php echo $name ?>

Done! Now go to http://homestead.app/sendemail, you should see:

Sent email successfully

Check your inbox, you should receive a new email!

Feel free to customize your email address, recipients, subjects, etc.

Note: This is a Chapter 3's section of Learning Laravel 5 book. You can read the book for free here: https://learninglaravel.net/laravel5