Skip to content
Home » Send Emails using AWS SES with PHP Code Examples in 2023

Send Emails using AWS SES with PHP Code Examples in 2023

In this article, we will discuss how to send emails using AWS Simple Email Service ( SES ) with PHP code examples. First, we will discuss what AWS SES is and its pricing plans. We will also discuss the prerequisites for sending emails.

In this article, we will cover the following topics.

  1. What is AWS Simple Email Service / SES?
  2. AWS SES Pricing.
  3. Prerequisites for Sending Emails Using AWS SES.
  4. Sending Emails Using Amazon SES Console.
  5. Sending emails Through Amazon SES using an AWS SDK
  6. What are the Pros and Cons of using AWS SES? 
  7. What are Alternatives to Amazon SES?

What is AWS Simple Email Service ( SES )?

Amazon SES is a cloud-based email service provider that can integrate into any application for sending emails. With Amazon Simple Email Service, you can send transactional emails, marketing messages, or any other type of high-quality content to your customers, and you pay only for what you use. 

It also provides a variety of deployments, including dedicated, shared, or owned IP addresses. And also, reports on sender statistics and a deliverability dashboard help businesses make every email count.

One of the best features of Amazon SES is its low cost. The AWS SES service is chosen and used by companies such as Netflix, Reddit, and Duolingo.

Another important feature is the integration, through the console itself, API, or SMTP, it is possible to configure the sending of emails. In addition, Amazon SES not only allows sending of emails but also receiving them.

Let’s look specifically at what it can be used for and the important features of AWS SES.

AWS SES Pricing

With Amazon SES, you pay based on the volume of emails sent and received. For more information, see the following table.

SERVICE TYPEVOLUME PER MONTHPRICEADDITIONAL CHARGES
Outbound email from EC20-62,000 emails

>62,000 emails
$0

$0.10/1000 emails
$0.12 for each GB of attachments you send.
Outbound emails from non-EC2Flat$0.10/1000 emails$0.12 for each GB of attachments you send.
Inbound email0-1,000 emails

>1,000 emails
$0

$0.10/1000 emails
$0.09 for every 1,000 incoming email chunks
AWS SES Pricing Plans

Now, let’s look step-by-step at how to set up Amazon SES to send emails.

Prerequisites for Sending Emails Using AWS SES with PHP

To set up AWS SES, you must complete the following prerequisites.

  • Sign up for AWS
  • Get your AWS access keys
  • Download an AWS SDK
  • Verify your email address
  • Create a shared credentials file

Sign Up For AWS

If you do not have an Amazon account, complete the following steps.

  1. Open and visit the link https://portal.aws.amazon.com/billing/signup.
  2. Follow the online instructions.

The sign-up procedure also involves receiving a phone call and entering a verification code on the phone keypad. 

An AWS account root user is created when you sign up for an AWS account. The root user can access all AWS services and resources in the account. 

Get Your AWS Access Keys

The AWS access keys contain an access key ID and a secret access key. When you sign up for AWS, you need to get your AWS access keys to access Amazon SES. You can get it through the AWS SES API, using the Query (HTTPS) interface directly or indirectly through an AWS SDK, the AWS Command Line Interface, or the AWS Tools for Windows PowerShell. 

Download an AWS SDK

You can use an AWS SDK to call the Amazon SES API without having to handle low-level details like assembling raw HTTP requests. The AWS SDKs provide functions and data types that can encapsulate the functionality of Amazon SES and other AWS services. To download an AWS SDK, go to SDKs

After downloading the SDK, you must create a shared credentials file and specify your AWS access keys.

Create a Shared Credentials File

You need to create a shared credentials file for the sample code to function correctly. To learn more, see creating a shared credentials file to use when sending email through Amazon SES using an AWS SDK.

Sending Emails Using Amazon SES Console

Following are the steps to send emails using the AWS SES console.

Step 1: Open The Amazon SES Console

First, you need to open the AWS SES console and search for SES.

Open AWS SES Console

Step 2: Verify Your Email Address

To send an email using Amazon SES, you must prove that you own the sender’s email address by verifying the address (or the domain of the address) with Amazon SES. 

And if you are a new user of Amazon SES, you must also verify the recipient’s address because your account is in a test environment called the Amazon SES sandbox. 

To verify your email, follow the following steps.

1. In the left navigation pane of your Amazon SES console, under Identity Management, select Email Addresses

AWS SES Setup
Select Email Addresses

2. Select Verify a New Email Address.

Amazon Simple Email Service Email Address Setup
Verify New Email

3. Enter your email, and then select Verify This Email Address.

Adding a New Email

Amazon SES will now send a verification email to that email address.

4. Now, sign in to the email client you use to receive an email for the email address you entered in the last step. Open the message from Amazon Web Services asking you to confirm that you own the email address. Click the link in the message.

Amazon verification Email

5. Go back to your Amazon SES console, and refresh the identity list by using the refresh button near the top right of the console. Now, confirm that the status of the email address in the Amazon SES console is verified.

Email Verified

Step 3: Send an Email

You can send an email once you have verified an email. With Amazon SES, you can send a formatted email or raw email. The Amazon SES will format the email for you if you choose the formatted email. And if you choose raw email, you must manually format the email, which gives you more control over the email headers and how the message displays. 

In this article, we use raw format to demonstrate how to send an email formatted in HTML.

Following are the steps to send a raw format email using AWS SES.

  1. Select the button to the left of your verified email address in your Amazon SES console, and then select Send a Test Email.
Selecting a Test Email

2. In the Send Test Email dialog box, use the following values and select Send Test Email.

Email Format – Choose Raw

To – Enter the email address you verified.

Message – Copy and paste the following text, exactly as shown, into the box.

AWS SES PHP Code Example
AWS Raw Format

3. Now, sign in to the email client of the address you sent the email to. You will find the message that you sent. If you cannot find it, check the spam folder.

Email Sent Successfully

How to send emails in PHP with Amazon SES using an AWS SDK

We can use an AWS SDK to send emails through Amazon SES. And the AWS SDKs are available for several programming languages. The prerequisites mentioned above must be completed to send emails using AWS SDK. 

Install the AWS SDK for PHP version 3

To download and install AWS SDK for PHP version 3, see the AWS SDK for PHP documentation

AWS Simple Email Service / SES PHP Code Example

To send an email through AWS SES using AWS SDK for PHP, use the following code.

<?php
 
// Modify the path in the required statement below to refer to the
// And the location of your Composer autoload.php file.
 
require 'vendor/autoload.php';
 
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
 
//Now Create a SesClient. And also change the value of the region parameter i
 
//If you use an AWS Region other than US West (Oregon).
 
//Also, change the value of the profile parameter if you want to use a profile in your credentials file
//other than the default.
 
$SesClient = new SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region'  => 'us-west-2'
]);
 
// Now replace the sender@example.com with your "From" address.
// And this address must be verified with your AWS SES.
 
$sender_email = 'sender@example.com';
 
// Now replace these sample addresses with the addresses of your recipients.
//And if your account is still in the sandbox, and these email addresses must be verified.
 
$recipient_emails = ['recipient_1@example.com','recipient_2@example.com'];
 
//Now, you need to specify the configuration set. And if you do not want to use a configuration.
 
// set, comment on the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
 
$configuration_set = 'ConfigSet';
 
$subject = 'Test Email';
$plaintext_body = 'The email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body =  '<h1>Amazon Simple Email Service Test Email</h1>';
$char_set = 'UTF-8';
 
try {
    $result = $SesClient->sendEmail([
        'Destination' => [
            'ToAddresses' => $recipient_emails,
        ],
        'ReplyToAddresses' => [$sender_email],
        'Source' => $sender_email,
        'Message' => [
          'Body' => [
              'Html' => [
                  'Charset' => $char_set,
                  'Data' => $html_body,
              ],
              'Text' => [
                  'Charset' => $char_set,
                  'Data' => $plaintext_body,
              ],
          ],
          'Subject' => [
              'Charset' => $char_set,
              'Data' => $subject,
          ],
        ],
       
        // And if you aren't using a configuration set, comment or delete the
        // following line
 
        'ConfigurationSetName' => $configuration_set,
    ]);
    $messageId = $result['MessageId'];
    echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
 
    // The output error message if it fails
 
    echo $e->getMessage();
    echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
    echo "\n";
}


Now, save the above code in a file named aws-ses-test.php. To run the program, open a command prompt in the same directory as aws-ses-test.php, and then type the following command.

$ php aws-ses-test,php

Now, review the output. If the email was successfully sent, the console displays “Email sent!” Otherwise, it displays an error message.

Why use AWS Simple Email Service / SES to Send Emails for Your PHP Web App

Let’s look at advantages and disadvantages of using AWS SES.

Pros of Using AWS SES to Send Emails with PHP.

One of the advantages of using AWS SES is low cost, and on a pay-per-use basis, you can use the service to send and receive emails and access other useful tools.

Another benefit of using AWS SES is the convenience of having a service with a high-level infrastructure without having to deal with it yourself.

And also, if you are already using Amazon AWS services such as Amazon EC2 and AWS Lambda, which also give you access to a limited number of free emails.

Find more about Amazon AWS prices and services.

Cons of Using AWS SES to Send Emails in PHP

There are limitations of Amazon SES that need to be pointed out. First, it is necessary to know that it is an emailing service and not a platform designed for marketing.

Among the main shortcomings highlighted by users of the service are the absence of templates and the inability to archive emails. As we said, regarding receiving emails, Amazon proposes integration with S3.

And another factor that can be frightening is the service configuration, which is necessary to start sending emails. 

What are Alternatives to Amazon SES?

Let’s look at some of the most widely used alternatives to the Amazon Simple Email Service.

Mailgun Transactional Email Service 

Mailgun is an email service that allows us to use the SMTP protocol or API to send emails. It also provides us with reports to track the number of emails sent and also allows us to check addresses for typos or spam problems.

Check out our review of Mailgun for sending emails with PHP

SendGrid Transactional Email Service

SendGrid is not just a service for sending emails but offers a complete email marketing suite with analytical tools to monitor email marketing campaigns. Among other features, it also provides the ability to use dedicated IPs. 

Large companies such as Uber, Spotify, and eBay use this email marketing API. Its biggest advantages are the high delivery rate and analytics dashboards. The free plan includes 100 emails per day. 

Check out our review of the SendGrid transactional email service

MailChimp Email Service

Mailchimp is a powerful marketing API service that provides you with intelligent marketing tools. It works with your favorite apps and web services and helps you do more with your email marketing. It can also find the tools you use or discover new ways for email marketing.

MailChimp allows us to create and send emails and offers us other features since it is a more comprehensive email marketing tool. Some examples are the ability to manage and import contact lists, send emails for abandoned carts, use a drag & drop editor, and schedule emails.

Mailchimp’s API client libraries and mobile SDKs make it easy to work with Mailchimp Marketing and Transactional APIs with your language or platform.

Using Amazon’s Simple Email Service / SES to Send Emails in PHP

This article explains what AWS Simple Email Service ( SES ) is and its pricing plans. It also describes how to use AWS SES and the prerequisites for sending emails.

This article also focuses on some frequently asked questions regarding AWS SES service, and we hope you find this article helpful.

I hope you’ve enjoyed the article. Stay tuned for more at FuelingPHP

Dig Deeper with Our Email in PHP Examples and Guides