Amazon Simple Email Service
Developer Guide

Send an Email Using the AWS SDK for PHP

This topic shows how to use the AWS SDK for PHP to send an email through Amazon SES.


In this tutorial, you send an email to yourself so that you can check to see if you received it. For further experimentation or load testing, use the Amazon SES mailbox simulator. Emails that you send to the mailbox simulator do not count toward your sending quota or your bounce and complaint rates. For more information, see Testing Amazon SES Email Sending.


Before you begin, perform the following tasks:

  • Verify your email address with Amazon SES—Before you can send an email with Amazon SES, you must verify that you own the sender's email address. If your account is still in the Amazon SES sandbox, you must also verify the recipient email address. The easiest way to verify email addresses is by using the Amazon SES console. For more information, see Verification Procedures.

  • Get your AWS credentials—You need an AWS access key ID and AWS secret access key to access Amazon SES using an SDK. You can find your credentials by using the Security Credentials page of the AWS Management Console. For more information about credentials, see Using Credentials With Amazon SES.

  • Install PHP—PHP is available at This tutorial requires PHP version 5.5 or higher. After you install PHP, add the path to PHP in your environment variables so that you can run PHP from any command prompt. The code in this tutorial was tested using PHP 7.0.20.

  • Install the AWS SDK for PHP version 3—For download and installation instructions, see the AWS SDK for PHP documentation. The code in this tutorial was tested using version 3.31.0 of the SDK.

  • Create a shared credentials file—For the sample code in this section to function properly, you must create a shared credentials file. For more information, see Create a Shared Credentials File.


The following procedure shows how to send an email through Amazon SES using the AWS SDK for PHP.

To send an email through Amazon SES using the AWS SDK for PHP

  1. In a text editor, create a file named amazon-ses-sample.php. Paste the following code:

    <?php // Replace path_to_sdk_inclusion with the path to the SDK as described in // define('REQUIRED_FILE','path_to_sdk_inclusion'); // Replace with your "From" address. // This address must be verified with Amazon SES. define('SENDER', ''); // Replace with a "To" address. If your account // is still in the sandbox, this address must be verified. define('RECIPIENT', ''); // Specify a configuration set. If you do not want to use a configuration // set, comment the following variable, and the // 'ConfigurationSetName' => CONFIGSET argument below. define('CONFIGSET','ConfigSet'); // Replace us-west-2 with the AWS Region you're using for Amazon SES. define('REGION','us-west-2'); define('SUBJECT','Amazon SES test (AWS SDK for PHP)'); define('HTMLBODY','<h1>AWS Amazon Simple Email Service Test Email</h1>'. '<p>This email was sent with <a href="">'. 'Amazon SES</a> using the <a href="">'. 'AWS SDK for PHP</a>.</p>'); define('TEXTBODY','This email was send with Amazon SES using the AWS SDK for PHP.'); define('CHARSET','UTF-8'); require REQUIRED_FILE; use Aws\Ses\SesClient; use Aws\Ses\Exception\SesException; $client = SesClient::factory(array( 'version'=> 'latest', 'region' => REGION )); try { $result = $client->sendEmail([ 'Destination' => [ 'ToAddresses' => [ RECIPIENT, ], ], 'Message' => [ 'Body' => [ 'Html' => [ 'Charset' => CHARSET, 'Data' => HTMLBODY, ], 'Text' => [ 'Charset' => CHARSET, 'Data' => TEXTBODY, ], ], 'Subject' => [ 'Charset' => CHARSET, 'Data' => SUBJECT, ], ], 'Source' => SENDER, // If you are not using a configuration set, comment or delete the // following line 'ConfigurationSetName' => CONFIGSET, ]); $messageId = $result->get('MessageId'); echo("Email sent! Message ID: $messageId"."\n"); } catch (SesException $error) { echo("The email was not sent. Error message: ".$error->getAwsErrorMessage()."\n"); } ?>
  2. In amazon-ses-sample.php, replace the following with your own values:

    • path_to_sdk_inclusion—Replace with the path required to include the AWS SDK for PHP in the program. For more information, see the AWS SDK for PHP documentation.

    •—Replace with an email address that you have verified with Amazon SES. For more information, see Verifying Email Addresses and Domains. Email addresses in Amazon SES are case-sensitive. Make sure that the address you enter is exactly the same as the one you verified.

    •—Replace with the address of the recipient. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox. Make sure that the address you enter is exactly the same as the one you verified.

    • (Optional) us-west-2—If you want to use Amazon SES in a Region other than US West (Oregon), replace this with the Region you want to use. For a list of Regions in which Amazon SES is available, see Regions and Amazon SES.

  3. Save amazon-ses-sample.php.

  4. To run the program, open a command prompt in the same directory as amazon-ses-sample.php, and then type php amazon-ses-sample.php

  5. Review the output. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.


    If you encounter a "cURL error 60: SSL certificate problem" error when you run the program, download the latest CA bundle as described in the AWS SDK for PHP documentation. Then, in amazon-ses-sample.php, add the following lines to the SesClient::factory array, replace path_of_certs with the path to the CA bundle you downloaded, and re-run the program.

    'http' => [ 'verify' => 'path_of_certs\ca-bundle.crt' ]
  6. Sign in to the email client of the recipient address. You will find the message that you sent.

On this page: