Menu
Amazon Simple Email Service
Developer Guide

Send an Email Through the Amazon SES SMTP Interface with PHP

This example uses the PHPMailer package 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 Composer dependency manager—The Composer dependency manager will enable you to download and install the PHPMailer class and its dependencies. To install Composer, follow the installation instructions at https://getcomposer.org/download.

  • Install the PHPMailer package— Once you have installed Composer, open the file composer.json in a text editor. In the require section, add the following line: "phpmailer/phpmailer":"~5.2", and then save the file. At the command line, change to the directory that contains the composer.json file, and then type php composer.phar update to download and install PHPMailer.

Procedure

The following procedure shows how to send an email through 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 // Modify the path in the require statement below to refer to the // location of your Composer autoload.php file. require './vendor/autoload.php'; // Instantiate a new PHPMailer $mail = new PHPMailer; // Tell PHPMailer to use SMTP $mail->isSMTP(); // 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. $mail->Host = 'email-smtp.us-west-2.amazonaws.com'; // Tells PHPMailer to use SMTP authentication $mail->SMTPAuth = true; // Replace smtp_username with your Amazon SES SMTP user name. $mail->Username = 'smtp_username'; // Replace smtp_password with your Amazon SES SMTP password. $mail->Password = 'smtp_password'; // Enable SSL encryption $mail->SMTPSecure = 'ssl'; // The port you will connect to on the Amazon SES SMTP endpoint. $mail->Port = 465; // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. $mail->setFrom('sender@example.com', 'Sender Name'); // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. // Also note that you can include several addAddress() lines to send // email to multiple recipients. $mail->addAddress('recipient@example.com', 'Recipient Name'); // Tells PHPMailer to send HTML-formatted email $mail->isHTML(true); // The subject line of the email $mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)'; // The HTML-formatted body of the email $mail->Body = '<h1>Email Test</h1> <p>This email was sent through the <a href="http://aws.amazon.com/ses/">Amazon SES</a> SMTP interface using the <a href="https://github.com/PHPMailer/PHPMailer"> PHPMailer</a> class.</p>'; // The alternative email body; this is only displayed when a recipient // opens the email in a non-HTML email client. The \r\n represents a // line break. $mail->AltBody = "Email Test\r\nThis email was sent through the Amazon SES SMTP interface using the PHPMailer class."; if(!$mail->send()) { echo 'Email not sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Email sent!'; }
  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. Sign in to the email client of the recipient address. You will find the message that you sent.

On this page: