Initial implementation of new Mail class and basic drivers

This commit is contained in:
Kijin Sung 2016-04-25 22:07:20 +09:00
parent 81b5230c9c
commit fedc7efc59
8 changed files with 1085 additions and 240 deletions

View file

@ -0,0 +1,72 @@
<?php
namespace Rhymix\Framework\Drivers\Mail;
/**
* The mail() function mail driver.
*/
class MailFunction 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.
*/
protected function __construct()
{
$this->mailer = \Swift_Mailer::newInstance(\Swift_MailTransport::newInstance());
}
/**
* Create a new instance of the current mail driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* 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)
{
$result = $this->mailer->send($message->message, $errors);
foreach ($errors as $error)
{
$message->errors[] = $error;
}
return (bool)$result;
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace Rhymix\Framework\Drivers\Mail;
/**
* The SMTP mail driver.
*/
class SMTP implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The mailer instance is stored here.
*/
protected $_mailer = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct(array $config)
{
$transport = \Swift_SmtpTransport::newInstance($config['host'], $config['port'], $config['secure']);
$transport->setUsername($config['user']);
$transport->setPassword($config['pass']);
$local_domain = $transport->getLocalDomain();
if (preg_match('/^\*\.(.+)$/', $local_domain, $matches))
{
$transport->setLocalDomain($matches[1]);
}
$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.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported()
{
return function_exists('proc_open');
}
/**
* 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)
{
$result = $this->mailer->send($message->message, $errors);
foreach ($errors as $error)
{
$message->errors[] = $error;
}
return (bool)$result;
}
}

View file

@ -0,0 +1,131 @@
<?php
namespace Rhymix\Framework\Drivers\Mail;
/**
* The SparkPost mail driver.
*/
class SparkPost 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.
*
* 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)
{
// Assemble the list of recipients.
$recipients = array();
if ($to = $message->message->getTo())
{
foreach($to as $address => $name)
{
$recipients[] = array('address' => array('name' => $name, 'email' => $address));
}
}
if ($cc = $message->message->getCc())
{
foreach($cc as $address => $name)
{
$recipients[] = array('address' => array('name' => $name, 'email' => $address));
}
}
if ($bcc = $message->message->getBcc())
{
foreach($bcc as $address => $name)
{
$recipients[] = array('address' => array('name' => $name, 'email' => $address));
}
}
// Prepare data and options for Requests.
$headers = array(
'Authorization' => $this->_config['api_token'],
'Content-Type' => 'application/json',
);
$data = json_encode(array(
'options' => array(
'transactional' => true,
),
'recipients' => $recipients,
'content' => array(
'email_rfc822' => $message->message->toString(),
),
));
$options = array(
'timeout' => 5,
'useragent' => 'PHP',
);
// Send the API request.
$request = \Requests::post(self::$_url, $headers, $data, $options);
$result = @json_decode($request->body);
// Parse the result.
if (!$result)
{
$message->errors[] = 'SparkPost: Connection error: ' . $request->body;
return false;
}
elseif ($result->errors)
{
foreach ($result->errors as $error)
{
$message->errors[] = 'SparkPost: ' . $error->message . ': ' . $error->description . ' (code ' . $error->code . ')';
}
}
if ($result->results)
{
return $result->results->total_accepted_recipients > 0 ? true : false;
}
else
{
return false;
}
}
}

View file

@ -0,0 +1,183 @@
<?php
namespace Rhymix\Framework\Drivers\Mail;
/**
* The Woorimail mail driver.
*/
class Woorimail implements \Rhymix\Framework\Drivers\MailInterface
{
/**
* The configuration is stored here.
*/
protected $_config = null;
/**
* The API URL.
*/
protected static $_url = 'https://woorimail.com:20080/index.php';
/**
* Error codes and messages.
*/
protected static $_error_codes = array(
'me_001' => '@ 없는 이메일 주소가 있습니다.',
'me_002' => '이메일 주소가 존재하지 않습니다.',
'me_003' => '닉네임이 존재하지 않습니다.',
'me_004' => '등록일이 존재하지 않습니다.',
'me_005' => '이메일과 닉네임 갯수가 다릅니다.',
'me_006' => '닉네임과 등록일 갯수가 다릅니다.',
'me_007' => '이메일과 등록일 갯수가 다릅니다.',
'me_008' => '이메일 갯수가 2,000개가 넘습니다.',
'me_009' => 'type이 api가 아닙니다.',
'me_010' => '인증키가 없습니다.',
'me_011' => '인증키가 부정확합니다.',
'me_012' => '포인트가 부족합니다.',
'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.
*
* 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)
{
// Assemble the POST data.
$data = array(
'type' => 'api',
'mid' => 'auth_woorimail',
'act' => 'dispWwapimanagerMailApi',
'title' => $message->getSubject(),
'content' => $message->getBody(),
'sender_email' => '',
'sender_nickname' => '',
'receiver_email' => array(),
'receiver_nickname' => array(),
'member_regdate' => date('YmdHis'),
'domain' => $this->_config['api_domain'],
'authkey' => $this->_config['api_token'],
'wms_domain' => 'woorimail.com',
'wms_nick' => 'NOREPLY',
'callback' => '',
'is_sendok' => 'W',
);
// Fill the sender info.
$from = $message->message->getFrom();
foreach($from as $email => $name)
{
$data['sender_email'] = $email;
$data['sender_nickname'] = $name;
break;
}
if(isset($this->_config['api_type']) && $this->_config['api_type'] === 'paid')
{
$sender_email = explode('@', $data['sender_email']);
if(count($sender_email) === 2)
{
$data['wms_nick'] = $sender_email[0];
$data['wms_domain'] = $sender_email[1];
}
}
// Fill the recipient info.
if ($to = $message->message->getTo())
{
foreach($to as $email => $name)
{
$data['receiver_email'][] = $email;
$data['receiver_nickname'][] = str_replace(',', '', $name);
}
}
if ($cc = $message->message->getCc())
{
foreach($cc as $email => $name)
{
$data['receiver_email'][] = $email;
$data['receiver_nickname'][] = str_replace(',', '', $name);
}
}
if ($bcc = $message->message->getBcc())
{
foreach($bcc as $email => $name)
{
$data['receiver_email'][] = $email;
$data['receiver_nickname'][] = str_replace(',', '', $name);
}
}
$data['receiver_email'] = implode(',', $data['receiver_email']);
$data['receiver_nickname'] = implode(',', $data['receiver_nickname']);
// Define connection options.
$options = array(
'timeout' => 5,
'useragent' => 'PHP',
);
// Send the API request.
$request = \Requests::post(self::$_url, array(), $data, $options);
$result = @json_decode($request->body);
// Parse the result.
if (!$result)
{
$message->errors[] = 'Woorimail: Connection error: ' . $request->body;
return false;
}
elseif($result->result === 'OK')
{
return true;
}
else
{
if(isset($result->error_msg))
{
if(isset(self::$_error_codes[$result->error_msg]))
{
$result->error_msg .= ' ' . self::$_error_codes[$result->error_msg];
}
$message->errors[] = 'Woorimail: ' . $result->error_msg;
}
else
{
$message->errors[] = 'Woorimail: Connection error';
}
return false;
}
}
}