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.If you do not have key set with you you can follow these steps to generate one for you.To get your access key ID and secret access key
- Open the IAM console at https://console.aws.amazon.com/iam/.
- On the navigation menu, choose Users.
- Choose your IAM user name (not the check box).
- Open the Security credentials tab, and then choose Create access key.
- To see the new access key, choose Show. Your credentials resemble the following:
- Access key ID:
AKIAIOSFODNN7EXAMPLE
- Secret access key:
wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
- Access key ID:
- To download the key pair, choose Download .csv file. Store the .csv file with keys in a secure location.
Note: The same set of
access key id
andsecret access key
can also be used for sending email via Amazon SMTP Creating an email address identity
Complete the following procedure to create an email address identity by using the Amazon SES console.
To create an email address identity (console)
- Sign in to the AWS Management Console and open the Amazon SES console at https://console.aws.amazon.com/ses/.
- In the navigation pane, under Configuration, choose Verified identities.
- Choose Create identity.
- Under Identity details, choose Email address as the identity type you want to create.
- For Email address, enter the email address that you want to use. The email address must be an address that’s able to receive mail and that you have access to.(Optional) If you want to Assign a default configuration set, select the check box.
- For Default configuration set, select the existing configuration set that you want to assign to your identity. If you haven’t created any configuration sets yet, see Using configuration sets in Amazon SES.
Note
Amazon SES only defaults to the assigned configuration set when no other set is specified at the time of sending. If a configuration set is specified, Amazon SES applies the specified set in place of the default set.
- For Default configuration set, select the existing configuration set that you want to assign to your identity. If you haven’t created any configuration sets yet, see Using configuration sets in Amazon SES.
- (Optional) Add one or more Tags to your domain identity by including a tag key and an optional value for the key:
- Choose Add new tag and enter the Key. You can optionally add a Value for the tag.
- Repeat for additional tags not to exceed 50, or choose Remove to remove tags.
- To create your email address identity, choose Create identity. After it’s created, you should receive a verification email within five minutes. The next step is to verify your email address by following the verification procedure in the next section.
- Mailing Writables as explained in the Laravel documentation
- Install Amazon AWS SDK for PHP in Laravel
composer require aws/aws-sdk-php
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, if it is not set already:... '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