Skip to content

Instantly share code, notes, and snippets.

@RWhar
Last active August 29, 2015 14:17
Show Gist options
  • Save RWhar/6aad873ba072bbddb564 to your computer and use it in GitHub Desktop.
Save RWhar/6aad873ba072bbddb564 to your computer and use it in GitHub Desktop.
Send email with Swift & SES
<?php
/**
* Requires
* "swiftmailer/swiftmailer": "5.*"
* "aws/aws-sdk-php": "2.7.25"
*/
use Aws\Common\Credentials\Credentials;
use Aws\Ses\SesClient;
require_once __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('Europe/London');
$e = Swift_Message::newInstance();
$e->setSubject('My Subject');
$e->setTo(['my@email.com' => 'me']);
$e->setFrom(['your@email' => 'you']);
$e->addPart('My email', 'text/plain');
$e->addPart('<h1>My Email</h1>', 'text/html');
$e->attach(Swift_Attachment::fromPath('/tmp/doc.pdf'));
$rawMessage = $e->toString();
$awsCredentials = new Credentials('...', '...');
$sesClient = SesClient::factory(['credentials' => $awsCredentials]);
$sentOk = $sesClient->sendRawEmail(['Data' => base64_encode($rawMessage)]);
if (!$sentOk) {
exit('Error while sending email');
}
echo 'Email sent';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment