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!

Step 1: Install AWS SDK for PHP

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

Step 2: Set up AWS SES

To send email using AWS SES, you need to set up an AWS SES account and verify your email address or domain.

AWS Create Identity

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.

Step 3: Configure Laravel to use AWS 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'),
    ],
];

Step 4: Set Environment Variables

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

 

Step 5: Configure Mail Settings

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...
];

Step 6: Create Mailable Class

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.

Step 7: Implement Mailable Class

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',
                    ]);
    }
}

Step 8: Create Email Template

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.

Step 8: Send Email

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.


Recent Questions

Recent Tutorials
Laravel 10 - Send mail using AWS SES

Learn to send Email using Laravel 10 with AWS SES service.

How to Make an AWS S3 Bucket Public

Learn how we can make an AWS S3 bucket public

Laravel 10 - Tutorial to create CRUD Application

Learn to create crud application using laravel 10. Step by step guidance will help to understand the concept of crud.

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.

Laravel 10 - Tutorial on how to use jQuery to validate the form

Learn how to use jQuery in Laravel 10 to validate the form