In this video we are going to configure our mail settings so that our Laravel we application can send emails.
Steps
Create free Postmark account
In order to send emails from our Laravel we app we need to use a 3rd party service. The Laravel documentations have detailed instructions on how to integrate with Postmark so we will use this. Simply your free Postmark account at https://www.postmarkapp.com/?via=codi... to get started.
Once you have created your Postmark account, we need to install Symfony's Postmark Mailer transport in our Laravel application. To do this, simply run "composer require symfony/postmark-mailer symfony/http-client" in the root of your project.
Now we need to set the default mailer in our mail.php config file. This file is found in the config folder within our Laravel project. Open the file and look for the 'default' key on the array. You will see it set to env('MAIL_MAILER', 'log'). This is an environment variable. So open your .env file and find MAIL_MAILER. Set this equal to 'postmark':
MAIL_MAILER=postmark
Now we need to make sure that the Postmark options are present in the services.php file in the config folder. Head to this file and make sure you see the postmark option. You will notice that 'token' is set to another environment variable called 'POSTMARK_TOKEN'. We will need to add this environment variable to our .env file. The actual Postmark token can be found in our Postmark account. From your default server (the one that was created when you created your account) click on the API Tokens tab. Copy the Server API token and paste it into your .env file:
POSTMARK_TOKEN=your-api-token-from-postmark
We are nearly there! Now we need to set our FROM_ADDRESS in the .env file. For now this must be the same email address we have used in our Postmark account, as we can only send emails from email addresses that have been verified with Postmark.
You have now set up emails! To test this, we can re-enable email verification in our Laravel application. Do this by heading to the fortify.php file in the config folder and uncommenting Features::emailVerification() in the features array. We also need to head to our User model (User.php) and uncomment MustVerifyEmail (at the top of the file) and add 'implements MustVerifyEmail' so that our class looks like this:
class User extends Authenticable implements MustVerifyEmail
Now when we try to view the dashboard page of our Laravel app when authenticated we will be redirected to the email verification page. This is due to the 'verified' middleware that is present on most of our routes. Click the link to resend the verification email. Then head back to your Postmark account and navigate to the Message Streams tab in your server. Then go to the Default Transactional Stream, and then from here click on the Activity tab. Here you will see the verification email sent by Laravel. If you do not yet see an email, wait a few seconds and refresh the page.
Congratulations, your Laravel web app can now send emails!
Table of contents
00:00 - Introduction
00:40 - What we need to do to configure mail
01:19 - Create free Postmark account
01:59 - Install Symfony's Postmark Mailer transport
02:33 - Set default mailer in mail config file
03:25 - Add Postmark options to services config
03:59 - Add Postmark API token to .env
05:01 - Test email sending using email verification
07:20 - Set from address in .env
09:25 - How to prevent emails going to spam folder