Last active
August 29, 2015 14:13
-
-
Save jayrambhia/ae1a8a5045f7e31b9361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require './inc/aws.phar'; | |
use Aws\Ses\SesClient; | |
/** | |
* SESUtils is a tool to make it easier to work with Amazon Simple Email Service | |
* Features: | |
* A client to prepare emails for use with sending attachments or not | |
* | |
* There is no warranty - use this code at your own risk. | |
* @author sbossen | |
* http://right-handed-monkey.blogspot.com | |
*/ | |
class SESMailer { | |
const version = "1.0"; | |
const AWS_KEY = "YOUR_KEY"; | |
const AWS_SEC = "YOUR_SECRET"; | |
const AWS_REGION = "us-east-1"; | |
const MAX_ATTACHMENT_NAME_LEN = 60; | |
/** | |
* Usage | |
* use $res->success to check if it was successful | |
* use $res->messageId to check later with Amazon for further processing | |
* use $res->error to look for error text if the task was not successful | |
* | |
* @param type $to - individual address or array of email addresses | |
* @param type $subject - UTF-8 text for the subject line | |
* @param type $body - Text for the email | |
* @param type $from - email address of the sender (Note: must be validated with AWS as a sender) | |
* @return \ResultHelper | |
*/ | |
public static function sendMail($params) { | |
$to = self::GetParam($params, 'to', true); | |
$subject = self::GetParam($params, 'subject', true); | |
$body = self::GetParam($params, 'message', true); | |
$from = self::GetParam($params, 'from', true); | |
$replyTo = self::GetParam($params, 'replyTo'); | |
$files = self::GetParam($params, 'files'); | |
$res = new ResultHelper(); | |
// get the client ready | |
$client = SesClient::factory(array( | |
'key'=> self::AWS_KEY, | |
'secret'=> self::AWS_SEC, | |
'region'=> self::AWS_REGION | |
)); | |
try { | |
// build the message | |
$msg = self::AddAddress('To', $to, true); | |
$msg .= self::AddAddress('From', $from, true); | |
$msg .= self::AddAddress('Reply-To', $replyTo); | |
// in case you have funny characters in the subject | |
$subject = mb_encode_mimeheader($subject, 'UTF-8'); | |
$msg .= "Subject: $subject\n"; | |
$msg .= "MIME-Version: 1.0\n"; | |
$msg .= "Content-Type: multipart/alternative;\n"; | |
$boundary = uniqid("_Part_".time(), true); //random unique string | |
$msg .= " boundary=\"$boundary\"\n"; | |
$msg .= "\n"; | |
// now the actual message | |
$msg .= "--$boundary\n"; | |
// first, the plain text | |
$msg .= "Content-Type: text/plain; charset=utf-8\n"; | |
$msg .= "Content-Transfer-Encoding: 7bit\n"; | |
$msg .= "\n"; | |
$msg .= strip_tags($body); | |
$msg .= "\n"; | |
// now, the html text | |
$msg .= "--$boundary\n"; | |
$msg .= "Content-Type: text/html; charset=utf-8\n"; | |
$msg .= "Content-Transfer-Encoding: 7bit\n"; | |
$msg .= "\n"; | |
$msg .= $body; | |
$msg .= "\n"; | |
// add attachments | |
if (is_array($files)) { | |
$count = count($files); | |
foreach ($files as $idx => $file) { | |
if ($idx !== 0) $msg .= "\n"; | |
$msg .= "--$boundary\n"; | |
$msg .= "Content-Transfer-Encoding: base64\n"; | |
$clean_filename = mb_substr($file["name"], 0, self::MAX_ATTACHMENT_NAME_LEN); | |
$msg .= "Content-Type: {$file['mime']}; name=$clean_filename;\n"; | |
$msg .= "Content-Disposition: attachment; filename=$clean_filename;\n"; | |
$msg .= "\n"; | |
$msg .= base64_encode(file_get_contents($file['filepath'])); | |
if (($idx + 1) === $count) $msg .= "==\n"; | |
$msg .= "--$boundary"; | |
} | |
// close email | |
$msg .= "--\n"; | |
} | |
// now send the email out | |
$ses_result = $client->sendRawEmail( | |
array( | |
'RawMessage'=> array( | |
'Data'=> base64_encode($msg) | |
) | |
), | |
array( | |
'Source'=> $from, | |
'Destinations'=> $to | |
) | |
); | |
if ($ses_result) { | |
$res->messageId = $ses_result->get('MessageId'); | |
} else { | |
$res->success = false; | |
$res->error = "Amazon SES did not return a MessageId"; | |
} | |
} catch (Exception $e) { | |
$res->success = false; | |
$res->error = $e->getMessage(); | |
} | |
return $res; | |
} | |
private static function GetParam($params, $param, $required = false) { | |
$value = isset($params[$param]) ? $params[$param] : null; | |
if ($required && empty($value)) { | |
throw new Exception('"' . $param . '" parameter is required.'); | |
} else { | |
return $value; | |
} | |
} | |
private static function AddAddress($kind, $address, $required = false) { | |
if ($address) { | |
if (is_array($address)) { | |
$address = rtrim(implode(',', $address), ','); | |
} | |
return $kind . ": $address\n"; | |
} else if ($required) { | |
throw new Exception($kind . ' address is required.'); | |
} else { | |
return ""; | |
} | |
} | |
} | |
class ResultHelper { | |
public $success = true; | |
public $error = ""; | |
public $messageId = ""; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment