-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
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
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'); | ||
} | ||
} |
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
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; | ||
} | ||
} |