Sending email using Amazon SES service

|
| By Webner

Sending email with or without Attachment using Amazon SES service

First download the aws library using the this link:

http://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip 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 = "notices@domain.com";
$reply_email = "abc@domain.com";
$mail_to_list = array("abc@abc.com","abc1@abc.com");
$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 = "";
if(!empty($to_str))
   $msg .= "To: $to_str".$rn;

if(!empty($cc_str))
   $msg .= "Cc: $cc_str".$rn;

if(!empty($bcc_str))
   $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));

Leave a Reply

Your email address will not be published. Required fields are marked *