Skip to content

Commit

Permalink
Add SES support
Browse files Browse the repository at this point in the history
  • Loading branch information
szeber committed Jan 10, 2017
1 parent 2043077 commit 437643e
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/Email/Ses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace ShoppinPal\YapepCommon\Email;

use Aws\Ses\SesClient;
use YapepBase\Config;
use YapepBase\Exception\ParameterException;

class Ses
{

protected $sesClient;

protected $senderEmail;

/**
* @param string $configName
*/
public function __construct($configName)
{
$config = Config::getInstance();
$region = $config->get('commonResource.ses.' . $configName . '.region', '');
$clientConfig = [
'version' => $config->get('commonResource.ses.' . $configName . '.version', 'latest'),
'credentials' => [
'key' => $config->get('commonResource.ses.' . $configName . '.accessKeyId'),
'secret' => $config->get('commonResource.ses.' . $configName . '.accessSecret'),
],
];

$this->senderEmail = $config->get('commonResource.ses.' . $configName . '.senderEmail');

if ($region) {
$clientConfig['region'] = $region;
}

$this->sesClient = new SesClient($clientConfig);
}

/**
* @param string $to
* @param string $subject
* @param string $htmlBody
* @param string $textBody
* @param array $ccAddresses
* @param array $bccAddresses
* @param string $charset
*
* @return mixed|null
* @throws ParameterException
*/
public function sendEmail($to, $subject, $htmlBody = null, $textBody = null, array $ccAddresses = [], $bccAddresses = [], $charset = 'UTF-8')
{
if (empty($htmlBody) && empty($textBody)) {
throw new ParameterException('Either the HTML or the text body must be set');
}

$message = [
'Subject' => [
'Charset' => $charset,
'Data' => $subject,
],
'Body' => [],
];

if ($htmlBody) {
$message['Body']['Html'] = [
'Charset' => $charset,
'Data' => $htmlBody,
];
}

if ($textBody) {
$message['Body']['Text'] = [
'Charset' => $charset,
'Data' => $textBody,
];
}

$args = [
'Destination' => [
'CcAddresses' => $ccAddresses,
'BccAddresses' => $bccAddresses,
'ToAddresses' => (array)$to,
],
'Message' => $message,
'Source' => $this->senderEmail,
];

$result = $this->sesClient->sendEmail($args);

return $result->get('MessageId');
}
}
46 changes: 46 additions & 0 deletions src/Email/SesFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ShoppinPal\YapepCommon\Email;

use YapepBase\Exception\ConfigException;

class SesFactory
{
/**
* @var array
*/
protected static $clients = [];

/**
* Returns an SES instance by the specified config name.
*
* @param string $configName
*
* @return Ses
*
* @throws ConfigException If the specified SES instance is not configured properly.
*/
public static function getClient($configName)
{
if (!isset(static::$clients[$configName])) {
static::$clients[$configName] = new Ses($configName);
}

return static::$clients[$configName];
}

/**
* Sets a client instance for the specified config name.
*
* Should only be used while testing!
*
* @param string $configName
* @param Ses $client
*
* @return void
*/
public static function setClient($configName, Ses $client)
{
static::$clients[$configName] = $client;
}
}

0 comments on commit 437643e

Please sign in to comment.