array( […]" /> Sending email using Amazon SES service Webner Blogs - eLearning, Salesforce, Web Development & More

Sending email using Amazon SES service

Author - Ashish Gautam

Sending email with or without Attachment using Amazon SES service

First download the aws library using the this link: link and save it in project.

Now the usage of library is below where we write the code to send email:

require "/Aws/aws-autoloader.php";
use Aws\Ses\SesClient;

// initialisation of variable
$sesClient = SesClient::factory(array(
               'credentials' => array(
                       'key'    => 'XXXXXXXXXXXXXXX',
                       'secret' => 'XXXXXXXXXXXXXXX',
               'region'=> 'us-east-1',
$rn = "\r\n";
$from = "";
$reply_email = "";
$mail_to_list = array("","");
$mail_cc_list = array();
$mail_bcc_list = array();
$subject = "subject";
$subject = htmlentities($subject);
$subject = mysqli_real_escape_string($conn,$subject);

$rand_num = rand(1000000,9999999);
$body = "Body of the email";
$file_arr = '';
$attachment = file_get_contents("attachemt.pdf");
$attachment_name = '_'.time().'.pdf';
//build the message
$to_str = '';
if (is_array($mail_to_list)) {
   $to_str = rtrim(implode(',', $mail_to_list), ',');
} elseif(!empty($mail_to_list)) {
   $to_str = $mail_to_list;

$cc_str = '';
if (is_array($mail_cc_list)) {
   $cc_str = rtrim(implode(',', $mail_cc_list), ',');
} elseif(!empty($mail_cc_list)) {
   $cc_str = $mail_cc_list;

$bcc_str = '';
if (is_array($mail_bcc_list)) {
   $bcc_str = rtrim(implode(',', $mail_bcc_list), ',');
} elseif(!empty($mail_bcc_list)) {
   $bcc_str = $mail_bcc_list;

$msg = "";
   $msg .= "To: $to_str".$rn;

   $msg .= "Cc: $cc_str".$rn;

   $msg .= "Bcc: $bcc_str".$rn;

$msg .="From: sender <$from>".$rn;
$msg .="Reply-To: $reply_email".$rn;
//in case you have funny characters in the subject
$subject = "subject";
$subject1 = mb_encode_mimeheader($subject, 'UTF-8');
$msg .="Subject: $subject1".$rn;
$msg .="MIME-Version: 1.0".$rn;
$msg .="Content-Type: multipart/alternative;".$rn;
$boundary = uniqid("_Part_".time(), true); //random unique string
$msg .=" boundary=\"$boundary\"".$rn;
$msg .=$rn;
//now the actual message
$msg .="--$boundary".$rn;
//first, the plain text
$msg .="Content-Type: text/plain; charset=utf-8".$rn;
$msg .="Content-Transfer-Encoding: 7bit".$rn;
$msg .=$rn;
$msg .=strip_tags($body);
$msg .=$rn;
//now, the html text
$msg .="--$boundary".$rn;
$msg .="Content-Type: text/html; charset=utf-8".$rn;
$msg .="Content-Transfer-Encoding: 7bit".$rn;
$msg .=$rn;
$msg .=$body;
$msg .=$rn;
//add attachments
if (!empty($attachment)) {
   $msg .="--$boundary".$rn;
   $msg .="Content-Transfer-Encoding: base64".$rn;
   $clean_filename = mb_substr($attachment_name, 0, 60);
   $msg .="Content-Type: Application/pdf; name=$clean_filename;".$rn;
   $msg .="Content-Disposition: attachment; filename=$clean_filename;".$rn;
   $msg .=$rn;
   $msg .=base64_encode($attachment);
   //only put this mark on the last entry
   if (!empty($file_arr))
       $msg .="==".$rn;
   $msg .="--$boundary";
//close email
$msg .="--".$rn;

// function to send email
$ses_result = $sesClient->sendRawEmail(array(
                       'RawMessage' => array('Data' => base64_encode($msg))), array('Source' => $from, 'Destinations' => $to_str));

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.