Laravel 10 - Send mail using AWS SES
Today, we're going to delve into the process of sending emails using the latest version of Laravel, Laravel 10, in conjunction with the powerful AWS SES service. This tutorial will also cover the essential steps to configure AWS SES for seamless email delivery.
Before we begin, let's quickly recap what Amazon SES is. Amazon SES, or Simple Email Service, is a cloud-based email sending service provided by Amazon Web Services (AWS). It provides a robust and scalable platform for sending and receiving emails over the internet. This service is widely utilized by businesses, developers, and organizations to manage their email communication efficiently.
Without further ado, let's dive right into the learning process!
To use AWS SES with Laravel, you need to install the AWS SDK for PHP via Composer. Run the following command to install the AWS SDK for PHP:
composer require aws/aws-sdk-php
To send email using AWS SES, you need to set up an AWS SES account and verify your email address or domain.
Note: Go to AWS support center to submit a service limit increase case, and choose SES Sending Limits, called ‘Remove it out of SandBox’ if not already done; Otherwise the capacity of the mail you are allowed to send and its rate will be limited to a large extend, besides that, all of the recipients will have to be verified by SES.
Edit the config/services.php
file in your Laravel project. If the file doesn't exist, create it. Add the AWS SES credentials:
// config/services.php
return [
// Other service configurations...
'ses' => [
'key' => env('AWS_SES_KEY'),
'secret' => env('AWS_SES_SECRET'),
'region' => env('AWS_SES_REGION'),
],
];
Open your .env
file in the root directory of your Laravel project and add the AWS SES credentials:
AWS_SES_KEY=your_aws_ses_key
AWS_SES_SECRET=your_aws_ses_secret
AWS_SES_REGION=your_aws_ses_region
Modify the config/mail.php
file to use the SES driver for sending emails:
// config/mail.php
return [
// Other mail configurations...
'mailers' => [
'ses' => [
'transport' => 'ses',
],
],
// Other mail configurations...
];
Generate a mailable class in your Laravel project. Run the following command in the terminal:
php artisan make:mail WelcomeEmail
This will create a WelcomeEmail.php
file inside the app/Mail
directory.
Open the WelcomeEmail.php
file in the app/Mail
directory and define the email content and settings:
// app/Mail/WelcomeEmail.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public function build()
{
return $this->from('sender@example.com')
->view('emails.welcome')
->with([
'name' => 'Recipient Name',
]);
}
}
Create an email template file (e.g., welcome.blade.php
) inside the resources/views/emails
directory. Customize the email template as needed, using Blade syntax.
Now, you can send the email using the created mailable class in your controllers or other parts of your application:
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;
Mail::mailer('ses')->to('recipient@example.com')->send(new WelcomeEmail());
Replace 'recipient@example.com'
with the recipient's email address.