Menu
Amazon Simple Email Service
Developer Guide

Amazon SES PHP Code Examples

This section contains the following sample code:

Sending Email Using the AWS SDK for PHP

The following sample code is a complete solution for sending email through Amazon SES using the AWS SDK for PHP. This sample code assumes that you have installed the AWS SDK for PHP, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

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"); } ?>

Sending Email through the Amazon SES SMTP Interface Using PHP

The following sample code is a complete solution for sending email through the Amazon SES SMTP interface using PHP. In order to run this sample code, you must obtain SMTP credentials; for more information, see Obtaining Your Amazon SES SMTP Credentials. You must also install the PHPMailer package using Composer.

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!'; }