Menu
Amazon Simple Email Service
Developer Guide

Send an Email Through Amazon SES Using the AWS SDK for PHP

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

Important

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.

Prerequisites

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 http://php.net/downloads.php. 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.

  • Install the AWS SDK for PHP version 3.x—For download and installation instructions, see the AWS SDK for PHP documentation.

  • 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.

Procedure

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:

    Copy
    <?php // Replace path_to_sdk_inclusion with the path to the SDK as described in // http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/basic-usage.html define('REQUIRED_FILE','path_to_sdk_inclusion'); // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. define('SENDER', 'sender@example.com'); // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. define('RECIPIENT', 'recipient@example.com'); // 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('BODY','This email was sent with Amazon SES using the AWS SDK for PHP.'); require REQUIRED_FILE; use Aws\Ses\SesClient; $client = SesClient::factory(array( 'version'=> 'latest', 'region' => REGION )); $request = array(); $request['Source'] = SENDER; $request['Destination']['ToAddresses'] = array(RECIPIENT); $request['Message']['Subject']['Data'] = SUBJECT; $request['Message']['Body']['Text']['Data'] = BODY; try { $result = $client->sendEmail($request); $messageId = $result->get('MessageId'); echo("Email sent! Message ID: $messageId"."\n"); } catch (Exception $e) { echo("The email was not sent. Error message: "); echo($e->getMessage()."\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.

    • sender@example.com—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.

    • recipient@example.com—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 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.

    Note

    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.

    Copy
    '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: