Sending emails using Laravel is quite easy. All you need to do is to configure your MAILER
service properly. In this simple tutorial I am going to show you two methods to configure your Laravel application to send emails with Amazon AWS SES
(Simple Emailing Service).
Prerequisites
- A working Laravel Applicaiton
- A working set of
AWS_ACCESS_KEY_ID
&AWS_SECRET_ACCESS_KEY
parameters. These parameters are required to send email via SES.
Note: The same set ofaccess key id
andsecret access key
can also be used for sending email via Amazon SMPT - Mailing Writables as explained in the Laravel documentation
Steps to configure Laravel Application to Send Email with Amazon AWS SES in Laravel
Method 1 – Configure SES as Mailer
- Open and edit your
.env
file which is found in the root folder of your Laravel Application - Add or update your
MAIL_MAILER
settings, to look like the followingMAIL_MAILER=ses
Add the following settings to
.env
as wellAWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxx AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxx
Hint: You can remove all other MAIL specific settings such as MAIL_HOST, MAIL_PORT etc from
.env
file.
- Open
config/mail.php
and set default mailer toses
... 'default' => env('MAIL_MAILER', 'ses'), ...
- Open
config/services.php
and setses
method:... 'default' => env('MAIL_MAILER', 'ses'), 'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), ], ...
Method 2 – Configure Amazon SMTP
If you have got Amazon SMTP settings you can add these smtp settings directly to .env file. In this method you don’t need to edit config/mail.php or config/services.php file of your Laravel Application. Just set in .env file as:
MAIL_MAILER=smtp MAIL_HOST=email-smtp.us-east-1.amazonaws.com MAIL_POR=587 MAIL_USERNAME=xxxxxxxxxxxxxxx MAIL_PASSWORD=xxxxxxxxxxxxxxx MAIL_ENCRYPTION=tls
Do Clear your Laravel Application cache and that should be it! An effective way to clear Laravel Applicaiton cache is demonstrated here.
Following above methods your Laravel application should be able to send messages via Amazon SES or Amazon SMTP service.
Also Read:
- How to Create folders recursively in Laravel (with Example)
- Laravel 8/9 Cron Job Task Scheduling Tutorial with examples
- How to Create Custom Log File in Laravel
- How to Clear cache in Laravel 9/8/7/6/5
- CSV Content Validation with FormRequest Rules in Laravel