Menu
Amazon Simple Email Service
Developer Guide (API Version 2010-12-01)

Send an Email Through the Amazon SES SMTP Interface with PHP

This example uses the PHP Extension and Application Repository (PEAR) to send email through Amazon SES using the SMTP interface.

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 SMTP credentials—You need an Amazon SES SMTP user name and password to access the Amazon SES SMTP interface. Your SMTP credentials are not the same as your AWS credentials. You can find your SMTP credentials by going to the SMTP Settings page of the Amazon SES console. For more information about SMTP credentials, see Obtaining Your Amazon SES SMTP Credentials.

  • Install PHP—PHP is available at http://php.net/downloads.php. 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 PEAR package manager—The PEAR package manager, which is available at https://pear.php.net/manual/en/installation.getting.php, will enable you to download the required PEAR packages.

  • Install the PEAR Mail and Net_SMTP packages— The PEAR Mail package is available at http://pear.php.net/package/Mail, and the PEAR Net_SMTP package is available at https://pear.php.net/package/Net_SMTP.

Procedure

The following procedure shows how to send an email through the Amazon SES with PHP.

To send an email using the Amazon SES SMTP interface with PHP

  1. Create a file named amazon-ses-smtp-sample.php. Open the file with a text editor and paste in the following code:

    Copy
    <?php // 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 smtp_username with your Amazon SES SMTP user name. define('USERNAME','smtp_username'); // Replace smtp_password with your Amazon SES SMTP password. define('PASSWORD','smtp_password'); // If you're using Amazon SES in a region other than US West (Oregon), // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP // endpoint in the appropriate region. define('HOST', 'email-smtp.us-west-2.amazonaws.com'); // The port you will connect to on the Amazon SES SMTP endpoint. define('PORT', '587'); // Other message information define('SUBJECT','Amazon SES test (SMTP interface accessed using PHP)'); define('BODY','This email was sent through the Amazon SES SMTP interface by using PHP.'); require_once 'Mail.php'; $headers = array ( 'From' => SENDER, 'To' => RECIPIENT, 'Subject' => SUBJECT); $smtpParams = array ( 'host' => HOST, 'port' => PORT, 'auth' => true, 'username' => USERNAME, 'password' => PASSWORD ); // Create an SMTP client. $mail = Mail::factory('smtp', $smtpParams); // Send the email. $result = $mail->send(RECIPIENT, $headers, BODY); if (PEAR::isError($result)) { echo("Email not sent. " .$result->getMessage() ."\n"); } else { echo("Email sent!"."\n"); } ?>
  2. In amazon-ses-smtp-sample.php, replace the following with your own values:

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

    • smtp_username—Replace with your SMTP user name credential, which you obtained from the SMTP Settings page of the Amazon SES console. This is not the same as your AWS access key ID. Note that your SMTP user name credential is a 20-character string of letters and numbers, not an intelligible name.

    • smtp_password—Replace with your SMTP password, which you obtained from the SMTP Settings page of the Amazon SES console. This is not the same as your AWS secret access key.

    • (Optional) email-smtp.us-west-2.amazonaws.com—If you want to use an Amazon SES SMTP endpoint in a region other than US West (Oregon), replace this with the Amazon SES SMTP endpoint in the region you want to use. For a list of Amazon SES SMTP endpoints, see Regions and Amazon SES.

  3. Save amazon-ses-smtp-sample.php.

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

  5. Review the output. If the sending succeeded, you will see "Email sent!"

  6. Log in to the email client of the recipient address. You will find the message that you sent.

On this page: