Send an email using SMTP with PHP - Amazon Simple Email Service

Send an email using SMTP with PHP

This example uses the PHPMailer class 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 email sending in Amazon SES.

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 Verifying email addresses in Amazon SES.

  • 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 class— After you install Composer, run the following command to install PHPMailer:

    path/to/composer require phpmailer/phpmailer

    In the preceding command, replace path/to/ with the path where you installed Composer.

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:

    <?php // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // If necessary, modify the path in the require statement below to refer to the // location of your Composer autoload.php file. require 'vendor/autoload.php'; // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. $sender = 'sender@example.com'; $senderName = 'Sender Name'; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. $recipient = 'recipient@example.com'; // Replace smtp_username with your Amazon SES SMTP user name. $usernameSmtp = 'smtp_username'; // Replace smtp_password with your Amazon SES SMTP password. $passwordSmtp = 'smtp_password'; // Specify a configuration set. If you do not want to use a configuration // set, comment or remove the next line. $configurationSet = 'ConfigSet'; // 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. $host = 'email-smtp.us-west-2.amazonaws.com'; $port = 587; // The subject line of the email $subject = 'Amazon SES test (SMTP interface accessed using PHP)'; // The plain-text body of the email $bodyText = "Email Test\r\nThis email was sent through the Amazon SES SMTP interface using the PHPMailer class."; // The HTML-formatted body of the email $bodyHtml = '<h1>Email Test</h1> <p>This email was sent through the <a href="https://aws.amazon.com/ses">Amazon SES</a> SMTP interface using the <a href="https://github.com/PHPMailer/PHPMailer"> PHPMailer</a> class.</p>'; $mail = new PHPMailer(true); try { // Specify the SMTP settings. $mail->isSMTP(); $mail->setFrom($sender, $senderName); $mail->Username = $usernameSmtp; $mail->Password = $passwordSmtp; $mail->Host = $host; $mail->Port = $port; $mail->SMTPAuth = true; $mail->SMTPSecure = 'tls'; $mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet); // Specify the message recipients. $mail->addAddress($recipient); // You can also add CC, BCC, and additional To recipients here. // Specify the content of the message. $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $bodyHtml; $mail->AltBody = $bodyText; $mail->Send(); echo "Email sent!" , PHP_EOL; } catch (phpmailerException $e) { echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer. } catch (Exception $e) { echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES. } ?>
  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 identities. 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) ConfigSet—If you want to use a configuration set when sending this email, replace this value with the name of the configuration set. For more information about configuration sets, see Using Amazon SES configuration sets.

    • (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 SMTP endpoint URLs for the AWS Regions where Amazon SES is available, see Amazon Simple Email Service (Amazon SES) in the AWS General Reference.

  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 then type php amazon-ses-smtp-sample.php.

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

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