Set Laravel Mailer with Mailtrap.io and Sendgrid.

Ariel Mejia - Aug 23 '19 - - Dev Community

Set email for logs

To add settings for emails Laravel provides an “.env” file, that we need to edit:

MAIL_DRIVER=log
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<mailtrapuser>
MAIL_PASSWORD=<mailtrappassword>
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=admin@yourapp.co
MAIL_FROM_NAME=YourApp

To try you can use the forget password link from artisan command "make:auth".

Go to “storage/logs” directory and you will find log file, in very bottom of the file you can see a preview in code of your email.


Set email for Develpment with Mailtrap.io

you only need to add “smtp” as driver, and add your username and password of mailtrap.io you can create a free account, and try again to submit another contact form.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<mailtrapuser>
MAIL_PASSWORD=<mailtrappassword>
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=admin@yourapp.co
MAIL_FROM_NAME=YourApp

If everything is correct you can access to your mailtrap.io inbox and you will see something like:

Alt Text

You can now use mailtrap to set all your emails for development.


Set email for Production

First we need to install a package https://github.com/s-ichikawa/laravel-sendgrid-driver to install it you only need the next command

$ composer require s-ichikawa/laravel-sendgrid-driver

When the package is installed, we need to add the service in “config/service.php” directory.

'sendgrid' => [
  'api_key' => env('SENDGRID_API_KEY'),
],

Now we need to change the “.env” file again to add the SendGrid Values, you can sign in with a free account in https://app.sendgrid.com and get 100 monthly emails for free or get a payment plan for more.

The username and password to “signin” or “register” are going to be used in “.env” file, and to generate an “apikey” you can go to settings on sidebar and click on “API keys” option, then press the blue button “Create API Key”, you can set any name to your api key, and in “API key permissions” by default is selected “Full access” so create it with “Full access”.

Alt Text

Alt Text

MAIL_DRIVER=smtp
SENDGRID_API_KEY='apikeyAsString'
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=<sendgridUser>
MAIL_PASSWORD=<sendgridPassword>
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=admin@yourapp.co
MAIL_FROM_NAME=YourApp

Now you can manage your different enviornments for development and production remeber sendgrid with free plan only works with 100 mails by month.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .