This is a sample way to integrate Amazon Simple Email Service. I have done this, by using PHP SDK provided by AWS.
Before that please ensure you have Amazon Web Service account.
Then get the Access Key and Secret Key from the IAM console which will be used to communicate with AWS.
Next step is, download AWS SDK for PHP from http://aws.amazon.com/sdk-for-php/ and extract the downloaded file and add the PHP SDK library file to your project.
Include concerned SDK libraries in your project as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | <?php require_once DOCUMENT_ROOT . '/aws/aws-autoloader.php'; require_once DOCUMENT_ROOT . '/aws/Aws/Ses/SesClient.php'; use \Aws\Ses\SesClient; function encodeRecipients($recipient){ $recipientsCharset = 'utf-8'; if (preg_match("/(.*)<(.*)>/", $recipient, $regs)) { $recipient = '=?' . $recipientsCharset . '?B?'.base64_encode($regs[1]).'?= <'.$regs[2].'>'; } return $recipient; } //SET AWS ACCESSKEY AND SECRETKEY $awsSesAccessKey = '<ACCESSKEY>'; $awsSesSecretKey = '<SECRETKEY>'; $strSesFromName = '<FROM USER NAME>'; $strSesFromEmail = '<FROM USER EMAIL>'; $strToMailName = '<TO USER NAME>'; $strToMail = '<TO USER EMAIL>' $strReplyToName = '<REPLYTO USER NAME>'; $strReplyToMail = '<REPLYTO USER EMAIL>'; $strBccName = '<Bcc USER NAME>'; $strBccMail = '<Bcc USER EMAIL>'; // Array of MAIL IDS to be added as Cc // Eg: $arrCCEmailIds = array('xyz@sample.com','abc@sample.com'); $arrCCEmailIds = array(); // Array of attachments /* Eg: $attachments = array( 0 => array( 'mimetype' => '<MIME TYPE OF FILE>', 'filename' => '<FILE NAME>', 'path' => '<PATH TO THE FILE>' ), 1 => array( 'mimetype' => '<MIME TYPE OF FILE>', 'filename' => '<FILE NAME>', 'path' => '<PATH TO THE FILE>' ) ); **/ $attachments = array(); $strSubject = '<SUBJECT OF EMAIL>'; $strMailTextVersion = '<PLAIN TEXT VERSION OF MAIL CONTENT>'; $strMailContent = '<HTML VERSION OF MAIL CONTENT>'; $objSes = SesClient::factory(array( 'key' => $awsSesAccessKey, 'secret' => $awsSesSecretKey, 'region' => 'us-east-1' )); $bSendRawMail = 0; $sesMail = array(); $strRawMessage = ""; $boundary = uniqid(rand(), true); $subjectCharset = $charset = 'utf-8'; $arrEmailDestins = array(); $strEmailCcs = ""; $strEmailReplyTo = ""; //Sending Raw Mail - Mail with Attachments if(is_array($attachments) && count($attachments) > 0){ $bSendRawMail = 1; $sesMail['Source'] = $strSesFromName . " <" . $strSesFromEmail . ">"; $sesMail['Destinations'] = array(); $sesMail['Subject'] = $strSubject; array_push($arrEmailDestins, $strToMailName . " <" . $strToMail . ">"); //Adding CC $strCcMailName = ''; if(is_array($arrCCEmailIds) && count($arrCCEmailIds) > 0){ foreach($arrCCEmailIds as $keyEmail => $valEmail){ $strCcMailName = substr($valEmail, 0, strrpos($valEmail,"@")); array_push($arrEmailDestins, $strCcMailName . " <" . $valEmail . ">"); $strEmailCcs .= encodeRecipients($strCcMailName . " <" . $valEmail . ">") . ", "; } } $strEmailCcs = substr($strEmailCcs, 0, (strlen($strEmailCcs) - 2)); if(trim($strReplyToMail) != "" && trim($strReplyToName) != ""){ $strEmailReplyTo = encodeRecipients($strReplyToName . " <" . $strReplyToMail . ">"); } //Specifying all destinations $sesMail['Destinations'] = $arrEmailDestins; //Generating Raw Message String $strRawMessage .= 'To: ' . encodeRecipients($strToMailName . " <" . $strToMail . ">") . "\n"; $strRawMessage .= 'From: '. encodeRecipients($strSesFromName . " <" . $strSesFromEmail . ">") . "\n"; if(trim($strEmailCcs) != ""){ $strRawMessage .= 'CC: '. $strEmailCcs . "\n"; } $strRawMessage .= 'BCC: '. encodeRecipients($strBccName . " <" . $strBccMail . ">") . "\n"; if(trim($strEmailReplyTo) != ""){ $strRawMessage .= 'Reply-To: '. $strEmailReplyTo . "\n"; } $strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\n"; $strRawMessage .= 'MIME-Version: 1.0' . "\n"; $strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\n"; //adding attachments foreach($attachments as $keyAt => $valAt){ $strRawMessage .= "\n--{$boundary}\n"; $strRawMessage .= 'Content-Type: '. $valAt['mimetype'] .'; name="'. $valAt['filename'].'";' . "\n"; $strRawMessage .= 'Content-Description: ' . $valAt['filename'] . ';' . "\n"; $strRawMessage .= 'Content-Disposition: attachment; filename="' . $valAt['filename'] . '"; size=' . filesize($valAt['path']). ';' . "\n"; $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\n\n"; $strRawMessage .= chunk_split(base64_encode(file_get_contents($valAt['path'])), 76, "\n") . "\n"; $strRawMessage .= '--' . $boundary . "\n"; } $strRawMessage .= "\n--{$boundary}\n"; $strRawMessage .= 'Content-Type: text/plain; charset=' . $charset . "\n"; $strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "\n\n"; $strRawMessage .= $strMailTextVersion . "\n"; $strRawMessage .= "--{$boundary}\n"; $strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\n"; $strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "\n\n"; $strRawMessage .= $strMailContent . "\n"; $sesMail['RawMessage']['Data'] = base64_encode($strRawMessage); } else{ //Sending Normal Mail - Mail without any attachments $sesMail['Source'] = $strSesFromName . " <" . $strSesFromEmail . ">"; $sesMail['Destination']['ToAddresses'][] = $strToMailName . " <" . $strToMail . ">"; //Adding CC $strCcMailName = ''; if(is_array($arrCCEmailIds) && count($arrCCEmailIds) > 0){ foreach($arrCCEmailIds as $keyEmail => $valEmail){ $strCcMailName = substr($valEmail, 0, strrpos($valEmail,"@")); $sesMail['Destination']['CcAddresses'][] = $strCcMailName . " <" . $valEmail . ">"; } } $sesMail['Destination']['BccAddresses'][] = $strBccName . " <" . $strBccMail . ">"; if(trim($strReplyToMail) != "" && trim($strReplyToName) != ""){ $sesMail['ReplyToAddresses'][] = $strReplyToName . " <" . $strReplyToMail . ">"; } $sesMail['Message']['Subject']['Data'] = $strSubject; $sesMail['Message']['Subject']['Charset'] = "UTF-8"; $sesMail['Message']['Body']['Text']['Data'] = $strMailTextVersion; $sesMail['Message']['Body']['Text']['Charset'] = "UTF-8"; $sesMail['Message']['Body']['Html']['Data'] = $strMailContent; $sesMail['Message']['Body']['Html']['Charset'] = "UTF-8"; } //Executing Send Mail try{ if($bSendRawMail == 1){ $result = $objSes->sendRawEmail($sesMail); } else{ $result = $objSes->sendEmail($sesMail); } } catch (Exception $ex) { $strErrorMailSubject = 'Amazon SES: Mail sending issue occurred - ' . date('d-m-Y H:i:s'); $strErrorMailContent = 'Mail Details: <br/><br/><br/>'; $strErrorMailContent .= 'Error Details: ' . $ex . ' <br/><br/>'; $strErrorMailContent .= 'To: '. $strToMail .' <br/>'; $strErrorMailContent .= 'Subject: '. $strSubject .' <br/>'; $strErrorMailContent .= 'Content: '. $strMailContent .' <br/>'; $to = 'xyz@sample.com'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: XYZ <xyz@sample.com>' . "\r\n"; $headers .= 'From: Error<errors@sample.com>' . "\r\n"; $headers .= 'Cc: abc@sample.com' . "\r\n"; mail($to, $strErrorMailSubject, $strErrorMailContent, $headers); } ?> |
If you have any queries on this add a comment to this post, so that I can help you to resolve the problems to integrate SES service.
Happy Emailing…