Setting up email can be a little confusing sometimes. This article will help clarify one of the most confusing aspects of this task: knowing where to find the information that you need! You can apply these principles across any email service provider, but I tend to use Mailgun for my sites. Let's dive in.
First, follow Krister Viirsaar's tutorial to get your DNS set up properly. His tutorial has been sponsored by Mailgun: Mailgun + DigitalOcean DNS Settings.
Mail is set up in Laravel's
config/mail.php file, but it is best practice to use our
.env file to store authentication information. Because
.env files are ignored by git (they had better be), we won't be sharing our information with the world if/when we push our codebase to GitHub or similar.
First, for context, let's take a look in our
config/mail.php file to see how Laravel uses these variables. No need to edit anything here, just take a look:
'mailers' => [ // smtp settings 'smtp' => [ 'transport' => 'smtp', 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('MAIL_PORT', 587), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, ], // api settings 'mailgun' => [ 'transport' => 'mailgun', // * 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), ],
*ensure that the
mailgun is present or an error will occur.
There are two options for sending mail through Mailgun, SMTP and via their API. According to Mailgun, using the API is "faster and more flexible," but they basically both do the same thing if you are looking to send mail messages. Either way we will be adding variables to our
.env file in order to make these connections. Let's see how to set these up.
MAIL_FROM_ADDRESSfirstname.lastname@example.org MAIL_FROM_NAME="Your Name" MAIL_MAILER=smtp # smtp MAIL_HOST=smtp.mailgun.org MAIL_PORT=2525 MAIL_USERNAMEemail@example.com MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxx-xxxxxxxx MAIL_ENCRYPTION=tls
Where to find SMTP info:
MAIL_FROM_ADDRESSfirstname.lastname@example.org MAIL_FROM_NAME="Your Name" MAIL_MAILER=mailgun # api MAILGUN_DOMAIN=mg.yourdomain.com MAILGUN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxx-xxxxxxxx
For the api settings, you will need to Add a Sending Key to your domain: