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,73 @@
<?php
namespace Rhymix\Framework\Drivers\Mail;
/**
* The base class for other mail drivers.
*/
abstract class Base implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The configuration is stored here.
*/
protected $_config = null;
/**
* The mailer instance is stored here.
*/
protected $_mailer = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct(array $config)
{
$this->_config = $config;
}
/**
* Create a new instance of the current mail driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
return new static($config);
}
/**
* Get the human-readable name of this mail driver.
*
* @return string
*/
public static function getName()
{
return class_basename(get_called_class());
}
/**
* 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 false;
}
/**
* 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)
{
return false;
}
}

View file

@ -5,18 +5,8 @@ namespace Rhymix\Framework\Drivers\Mail;
/**
* The mail() function mail driver.
*/
class MailFunction implements \Rhymix\Framework\Drivers\MailInterface
class MailFunction extends Base implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* The mailer instance is stored here.
*/
protected $_mailer = null;
/**
* Direct invocation of the constructor is not permitted.
*/
@ -26,18 +16,13 @@ class MailFunction implements \Rhymix\Framework\Drivers\MailInterface
}
/**
* Create a new instance of the current mail driver, using the given settings.
* Get the human-readable name of this mail driver.
*
* @param array $config
* @return void
* @return string
*/
public static function getInstance(array $config)
public static function getName()
{
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
return 'PHP mail()';
}
/**
@ -62,7 +47,16 @@ class MailFunction implements \Rhymix\Framework\Drivers\MailInterface
*/
public function send(\Rhymix\Framework\Mail $message)
{
$result = $this->mailer->send($message->message, $errors);
try
{
$result = $this->mailer->send($message->message, $errors);
}
catch(\Exception $e)
{
$message->errors[] = $e->getMessage();
return false;
}
foreach ($errors as $error)
{
$message->errors[] = $error;

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;
}
}
}

View file

@ -5,13 +5,8 @@ namespace Rhymix\Framework\Drivers\Mail;
/**
* The SMTP mail driver.
*/
class SMTP implements \Rhymix\Framework\Drivers\MailInterface
class SMTP extends Base implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The mailer instance is stored here.
*/
protected $_mailer = null;
/**
* Direct invocation of the constructor is not permitted.
*/
@ -28,17 +23,6 @@ class SMTP implements \Rhymix\Framework\Drivers\MailInterface
$this->mailer = \Swift_Mailer::newInstance($transport);
}
/**
* Create a new instance of the current mail driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
return new self($config);
}
/**
* Check if the current mail driver is supported on this server.
*
@ -61,7 +45,16 @@ class SMTP implements \Rhymix\Framework\Drivers\MailInterface
*/
public function send(\Rhymix\Framework\Mail $message)
{
$result = $this->mailer->send($message->message, $errors);
try
{
$result = $this->mailer->send($message->message, $errors);
}
catch(\Exception $e)
{
$message->errors[] = $e->getMessage();
return false;
}
foreach ($errors as $error)
{
$message->errors[] = $error;

View file

@ -5,37 +5,13 @@ namespace Rhymix\Framework\Drivers\Mail;
/**
* The SparkPost mail driver.
*/
class SparkPost implements \Rhymix\Framework\Drivers\MailInterface
class SparkPost extends Base implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The configuration is stored here.
*/
protected $_config = null;
/**
* The API URL.
*/
protected static $_url = 'https://api.sparkpost.com/api/v1/transmissions';
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct(array $config)
{
$this->_config = $config;
}
/**
* Create a new instance of the current mail driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
return new self($config);
}
/**
* Check if the current mail driver is supported on this server.
*

View file

@ -5,13 +5,8 @@ namespace Rhymix\Framework\Drivers\Mail;
/**
* The Woorimail mail driver.
*/
class Woorimail implements \Rhymix\Framework\Drivers\MailInterface
class Woorimail extends Base implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The configuration is stored here.
*/
protected $_config = null;
/**
* The API URL.
*/
@ -36,25 +31,6 @@ class Woorimail implements \Rhymix\Framework\Drivers\MailInterface
'me_013' => '전용채널에 도메인이 등록되어 있지 않습니다.',
);
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct(array $config)
{
$this->_config = $config;
}
/**
* Create a new instance of the current mail driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
return new self($config);
}
/**
* Check if the current mail driver is supported on this server.
*