• By :  Piyush Rishi Singh
  • Last Updated :  December 2, 2022
  • Words Count :  518 Words
  • Est. Reading Time :  4 mins

How to send mail with Amazon SES Smtp using PhpMailer

At first, let's check you've done the basics, the routine:
1. You need to log in to AWS, to choose SES tab, and in the left menu to choose “Verified Senders”. You have to verify at least one email address you will send emails from (for our example will use example@example.com). Once you're done, move to SMTP settings.
2. In SMTP setting grab your SMTP credentials. You'll need:

  • Server name (for me it's email-smtp.us-east-1.amazonaws.com)
  • Port (25, 465 or 587)
  • Authentication (create you own by following button “Create My SMTP credentials”) You'll get Username and Password

You're done with ASW SES settings! Maybe you'll need to go to Dashbord to check your sending limits, if you see the limit is to tight for you, just request increase from AWS support (Contact form), they will increase it for you.

Now let's move to PHPMailer

1. Download the latest version at http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list
unpack it to phpmailer folder.
2. Create new file named Mailer.php that will extend PHPMailer class


< ?php
 include_once('phpmailer/class.phpmailer.php'); 
 
 class Mailer extends PHPMailer{ 
 
     var $Host = "tls://email-smtp.us-east-1.amazonaws.com"; //Amazon Server
     var $Username = "xxxxxxxxxxxxxxxxxx";    //Enter Username from Amazon 
     var $Password = "xxxxxxxxxxxxxxxxxxx";  //Enter Pasword from Amazon 
     var $Port = 465;                        //Port number from Amazon 
     var $SMTPAuth = true; 
     var $From = "myportal@example.com";    //From may be any email you want 
     var $FromName = "My Site"; 
     var $CharSet = "UTF-8"; 
     var $Sender = 'example@example.com';  //Sender has to be verified email !!! 
 
     var  $SMTPDebug  = false;             // set true if you want to debug
  /** 
   * Prevents the SMTP connection from being closed after each mail 
   * sending. If this is set to true then to close the connection 
   * requires an explicit call to SmtpClose(). 
   * @var bool 
   */
      var $SMTPKeepAlive = true; 
     function Mailer(){ 
           $this->IsSMTP(); 
      } 
      function send_email($email,$subject,$body){ 
              $this->ClearAddresses(); 
                $this->SingleTo = true;               
                mb_internal_encoding("UTF-8");  //If you send in UTF-8 Encoding 
                $this->AddAddress($email); 
                $this->Subject = ($subject); 
                $this->Body=$body; 
                return $this->Send();
      }
 }
 ?> 

3. Include this file in you application and call each time you want to send email:


< ?php
   include_once('Mailer.php'); 
   $mailer=new Mailer(); 
   $mailer->send_email('myprivate@email.com','First email','Hello world!'); 
 ?> 

You're done, you're ready to send emails using Amazon SES with PHPmailer. 
Since you've started to use amazon SES you will encounter several problems:

1. Despite you'll see high numbers in your Amazon's sending limits you will be surprised how slow is sending itself.
So:
First. Don't send emails directly with user request (from forms for example). Save request and send emails in background with cron after that. Users will thank you.
Second. We have Max Send Rate 28 emails/second, but without asynchronous approach we won't be able to rich the limit! Our max sending rate in one thread is one in 3 seconds!

2. Amazon SES very strict with Bounced emails and Complaints, they will block your account if you're not dealing with this emails. Clean up your database from invalid emails, check Bounced and Complaints returning emails. It might be done easily by using Amazon SNS  Notifications center.

Share :
Piyush Rishi Singh

Piyush Rishi Singh

www.piyushrishisingh.com
(Founder & Business Head At Techzax Innovations Pvt. Ltd.)

A Digital Entrepreneur & Content Creator who loves simplifying tech.
Expertise: A Full Stack & highly skilled 10+ years experienced WordPress developer who specializes in complete custom tailored WordPress Websites development.

Table Of Contents