Reorganize mail driver class structure and add SES driver

This commit is contained in:
Kijin Sung 2016-05-06 21:31:04 +09:00
parent a12722ad79
commit 0420361349
8 changed files with 189 additions and 90 deletions

View file

@ -0,0 +1,80 @@
<?php
namespace Rhymix\Framework\Drivers\Mail;
/**
* The Amazon SES mail driver.
*/
class SES extends Base implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* Cache the message here for debug access.
*/
protected $_message;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct(array $config)
{
$transport = \Swift_AWSTransport::newInstance($config['user'], $config['pass']);
$transport->setDebug(array($this, 'debugCallback'));
$transport->setEndpoint('https://email.' . strtolower($config['type']) . '.amazonaws.com/');
$this->mailer = \Swift_Mailer::newInstance($transport);
}
/**
* Check if the current mail driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported()
{
return true;
}
/**
* Send a message.
*
* This method returns true on success and false on failure.
*
* @param object $message
* @return bool
*/
public function send(\Rhymix\Framework\Mail $message)
{
$this->_message = $message;
try
{
$result = $this->mailer->send($message->message, $errors);
}
catch(\Exception $e)
{
$message->errors[] = $e->getMessage();
return false;
}
foreach ($errors as $error)
{
$message->errors[] = $error;
}
return (bool)$result;
}
/**
* Debug callback function for SES transport.
*
* @param string $msg
* @return void
*/
public function debugCallback($msg)
{
if ($this->_message)
{
$this->_message->errors[] = $msg;
}
}
}