Skip to content

Instantly share code, notes, and snippets.

@kimpepper
Created March 15, 2016 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimpepper/2171755ed40d3d7221fd to your computer and use it in GitHub Desktop.
Save kimpepper/2171755ed40d3d7221fd to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains Drupal\ses_mailer\SesMailer
*/
namespace Drupal\ses_mailer;
use Aws\Exception\AwsException;
use Aws\Sdk;
/**
* AWS SES mailer implementation.
*/
class SesMailer implements \MailSystemInterface {
/**
* {@inheritdoc}
*/
public function format(array $message) {
// Join the body array into one string.
$message['body'] = implode("\n\n", $message['body']);
// Convert any HTML to plain-text.
$message['body'] = drupal_html_to_text($message['body']);
// Wrap the mail body for sending.
$message['body'] = drupal_wrap_mail($message['body']);
return $message;
}
/**
* {@inheritdoc}
*/
public function mail(array $message) {
$settings = [
'region' => variable_get('ses_mailer_region', 'us-east-1'),
'version' => 'latest',
];
$profile = variable_get('ses_mailer_profile');
if (isset($profile)) {
$settings['profile'] = $profile;
}
$sdk = new Sdk($settings);
try {
// Credentials are set in environment variables.
$client = $sdk->createSes();
$client->sendEmail([
'Destination' => [
'ToAddresses' => [$message['to']],
],
'Message' => [
'Body' => [
'Text' => [
'Data' => $message['body'],
],
],
'Subject' => [
'Data' => $message['subject'],
],
],
'ReplyToAddresses' => [$message['headers']['From']],
'ReturnPath' => $message['headers']['Return-Path'],
'Source' => $message['headers']['Sender'],
]
);
watchdog('ses_mailer', 'Successfully sent email');
}
catch (AwsException $e) {
watchdog_exception('ses_mailer', $e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment