Email Using Phpmailer and Amazon SES in PHP | AWS SES
Send an Email Using Phpmailer and Amazon SES in PHP with Source Code

 Hello Coders

In this article, I am going to share the complete knowledge & source code of Amazon SES in PHP technology. we are going to use Phpmailer class along with the SES email script to send the email.

Basic Configuration

Before we start, we need to follow these steps.

1. Verify your email id(address) with Amazon SES

In this step, we need to verify our email address (Sender which means you own the sender email id). If you own Gmail then you can verify your Gmail email id and then you can start sending emails using Amazon SES.
 If you are using the Amazon SES sandbox account, you also need to verify the recipient's email address.
In Sandbox, you can use all features of SES but with some restrictions and those are:-

-You can only send mail to verified email addresses and domains.

-You can send a maximum of 200 messages per 24-hour period.

-You can send a maximum of 1 message per second.

For moving the SES sandbox to SES live version, you have to submit a request to the Amazon support center. check process here – Click here

To check if your account is in the sandbox

A. Go to Amazon SES console – Click here
B. Navigate the Region selector to choose an AWS Region
C. In the navigation pane, under Email Sending, choose Sending Statistics
D. If your account is still in the sandbox in the AWS Region that you selected, you will see a banner at the top of the page that resembles the example in the following image.

Amazon SES

 

The easiest way to verify email addresses is by using the Amazon SES console. For more information, see Verifying email addresses in Amazon SES.

Process for the verification of Email address:-

A. Go to the Amazon SES console – Click here
B. Navigate the Region selector and choose an AWS Region
C. In the navigation pane, under Identity Management, choose Email Addresses
D. Click on Verify a New Email Address. Enter your sender or receiver email (if your account in the sandbox).

E. After sometime Amazon will send a verification email to your email address. You can verify it from there. You can check the verification status in the Email Addresses.

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

To create your SMTP credentials

A. Sign in to the AWS Management Console and open the Amazon SES console at  https://console.aws.amazon.com/ses/

B. In the navigation pane, choose SMTP Settings.

C. In the content pane, choose to Create My SMTP Credentials.

D. For Create User for SMTP, type a name for your SMTP user. Alternatively, you can use the default value that is provided in this field. When you finish, choose to Create.

  • Choose Show User SMTP Credentials. Your SMTP credentials are shown on the screen. Download or copy these credentials and store them in a safe place, as you cannot view or save your credentials after you dismiss this dialog box.

  • You can view a list of existing SMTP credentials that you've created using this procedure by going to the IAM console at  https://console.aws.amazon.com/iam/. In the navigation pane, under Access management, choose Users. Use the search bar to find all users that contain the text "ses-smtp-user".
     

3. Install PHP & Install the Composer dependency manager

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

composer require phpmailer/phpmailer
  • Install the PHPMailer class— After you install Composer, run the following command to install PHPMailer:

After this, you can use this script and phpmailer class to send an email using amazon ses credentials

<?php
/**
 * This example shows making an SMTP connection with authentication.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'phpmailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "-- Put SMTP Username --";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
//$mail->Username = "-- Put Username --";
$mail->Username = "-- Put  Username --";
//Password to use for SMTP authentication
//$mail->Password = "-- Put SMTP Password--";
$mail->Password = "-- Put SMTP Password--";
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'SES');
//Set an alternative reply-to address
//$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("This is the testing email for smtp");
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

 

Download complete code of Amazon SES with PHPMailer