Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created January 4, 2012 04:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xeoncross/1558464 to your computer and use it in GitHub Desktop.
Save xeoncross/1558464 to your computer and use it in GitHub Desktop.
Simple SendEmail wrapper for Amazon SES API (does not support SendRawEmail)
<?php
class SES
{
public $accessKey = NULL;
public $accessKeyID = NULL;
public $host = 'https://email.us-east-1.amazonaws.com/';
public $userAgent = 'Test SES Class / v1.0';
public $data = array();
public $nl = "\r\n";
/**
* The To: field(s) of the message.
*
* @param string|array $addresses to send email to
* @return this
*/
public function to($addresses)
{
foreach( (array) $addresses as $i => $address)
{
$this->data['Destination.ToAddresses.member.' . ($i+1)] = $address;
}
return $this;
}
/**
* The CC: field(s) of the message.
*
* @param string|array $addresses to copy
* @return this
*/
public function cc($addresses)
{
foreach( (array) $addresses as $i => $address)
{
$this->data['Destination.CcAddresses.member.' . ($i+1)] = $address;
}
return $this;
}
/**
* The BCC: field(s) of the message.
*
* @param string|array $addresses to blind copy
* @return this
*/
public function bcc($addresses)
{
foreach( (array) $addresses as $i => $address)
{
$this->data['Destination.BccAddresses.member.' . ($i+1)] = $address;
}
return $this;
}
/**
* The reply-to email address(es) for the message. If the recipient replies
* to the message, each reply-to address will receive the reply.
*
* @param string|array $addresses to reply to
* @return this
*/
public function replyTo($addresses)
{
foreach( (array) $addresses as $i => $address)
{
$this->data['ReplyToAddresses.member.' . ($i+1)] = $address;
}
return $this;
}
/**
* The sender's email address.
*
* @param string $address of sender
* @return this
*/
public function from($address)
{
$this->data['Source'] = $address;
return $this;
}
/**
* The email address to which bounce notifications are to be forwarded.
*
* @param string $address to send bounced emails
* @return this
*/
public function returnPath($address)
{
$this->data['ReturnPath'] = $address;
return $this;
}
/**
* Email message subject
*
* @param string $subject of email
* @param string $charset of subject
* @return this
*/
public function subject($subject, $charset = 'UTF-8')
{
$this->data['Message.Subject.Data'] = $subject;
$this->data['Message.Subject.Charset'] = $charset;
return $this;
}
/**
* HTML version of email
*
* @param string $html version of message
* @param string $charset of subject
* @return this
*/
public function html($html, $charset = 'UTF-8')
{
$this->data['Message.Body.Html.Data'] = $html;
$this->data['Message.Body.Html.Charset'] = $charset;
return $this;
}
/**
* Text version of email
*
* @param string $text version of message
* @param string $charset of subject
* @return this
*/
public function text($text, $charset = 'UTF-8')
{
$this->data['Message.Body.Text.Data'] = $text;
$this->data['Message.Body.Text.Charset'] = $charset;
return $this;
}
/**
* Send the email
*
* @param string $html version of message
* @param string $charset of subject
* @return this
*/
public function sendEmail()
{
$this->data['Action'] = 'SendEmail';
return $this->request($this->data, FALSE);
}
/**
* Send HTTP(S) request to AWS
*
* @param array $data to send
* @param boolean $post to send POST request
* @return object
*/
public function request($data = NULL, $post = TRUE)
{
$date = gmdate('D, d M Y H:i:s O');
$headers = array(
'X-Amzn-Authorization: '. $this->headerSignature($date),
'Date: ' . $date,
//'Host: ' . $this->host,
//'Connection: close',
);
$url = $this->host;
$options = array(
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_HEADER => TRUE
);
if($post)
{
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = http_build_query($data, '', '&');
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
//$headers[] = 'Content-Length: ' . strlen($options[CURLOPT_POSTFIELDS]);
}
else
{
$url .= '?'. http_build_query($data, '', '&');
}
$options[CURLOPT_HTTPHEADER] = $headers;
$result = curl_request($url, $options);
// Return response as object
if($result->body AND ! $result->error)
{
libxml_use_internal_errors(TRUE);
if($xml = simplexml_load_string($result->body))
{
return json_decode(json_encode($xml));
}
}
return $result;
}
/**
* Clear the current email settings.
*
* @return this
*/
public function clear()
{
$this->data = array();
return $this;
}
/**
* Caculate AWS HMAC header
*
* @param string $date
* @return string
*/
protected function headerSignature($date)
{
return 'AWS3-HTTPS AWSAccessKeyId=' . $this->accessKeyID . ',Algorithm=HmacSHA256,Signature='
. base64_encode(hash_hmac('sha256', $date, $this->accessKey, true));
}
}
/**
* Make a request to the given URL using cURL.
*
* @param string $url to request
* @param array $options for cURL object
* @return object
*/
function curl_request($url, array $options = NULL)
{
$ch = curl_init($url);
$defaults = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 10,
);
// Connection options override defaults if given
curl_setopt_array($ch, (array) $options + $defaults);
// Create a response object
$object = new stdClass;
// Get additional request info
$object->body = curl_exec($ch);
$object->error_code = curl_errno($ch);
$object->error = curl_error($ch);
$object->info = curl_getinfo($ch);
curl_close($ch);
return $object;
}
/*
* Now actually do something
*/
$ses = new SES;
$ses->accessKey = '';
$ses->accessKeyID = '';
$ses->to('user@example.com')
->from('owner@example.com')
->subject('Text email')
->html("<h3>Hello World!</h3>\n<p><b>Way</b> cool man!</p>\n")
->text("## Hello World!\nWay cool man!");
$result = $ses->sendEmail();
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment