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

@ -68,6 +68,7 @@ return array(
'smtp_pass' => null,
'api_domain' => null,
'api_token' => null,
'api_type' => null,
'api_user' => null,
'api_pass' => null,
),

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

View file

@ -0,0 +1,36 @@
<?php
namespace Rhymix\Framework\Drivers;
/**
* The mail driver interface.
*/
interface MailInterface
{
/**
* Create a new instance of the current mail driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $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();
/**
* 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);
}

567
common/framework/mail.php Normal file
View file

@ -0,0 +1,567 @@
<?php
namespace Rhymix\Framework;
/**
* The mail class.
*/
class Mail
{
/**
* Instance properties.
*/
public $message = null;
public $driver = null;
public $content_type = 'text/html';
public $attachments = array();
public $errors = array();
/**
* Static properties.
*/
public static $default_transport = null;
/**
* Set the default transport.
*
* @param object $transport
* @return void
*/
public static function setDefaultTransport(Drivers\MailInterface $transport)
{
self::$default_transport = $transport;
}
/**
* Get the default transport.
*
* @param object $transport
* @return void
*/
public static function getDefaultTransport()
{
if (!self::$default_transport)
{
self::$default_transport = Drivers\Mail\MailFunction::getInstance();
}
return self::$default_transport;
}
/**
* Get the list of supported mail drivers.
*
* @return array
*/
public static function getSupportedDrivers()
{
$result = array();
foreach (Storage::readDirectory(__DIR__ . '/drivers/mail', false) as $filename)
{
$driver_name = substr($filename, 0, -4);
$class_name = '\Rhymix\Framework\Drivers\Mail\\' . $driver_name;
if ($class_name::isSupported())
{
$result[] = $driver_name;
}
}
return $result;
}
/**
* The constructor.
*/
public function __construct()
{
$this->message = \Swift_Message::newInstance();
$this->driver = self::getDefaultTransport();
}
/**
* Set the sender (From:).
*
* @param string $email E-mail address
* @param string $name Name (optional)
* @return bool
*/
public function setFrom($email, $name = null)
{
try
{
$this->message->setFrom($name === null ? $email : array($email => $name));
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Get the sender (From:).
*
* @return string|null
*/
public function getFrom()
{
$list = $this->formatAddresses($this->message->getFrom());
return $list ? array_first($list) : null;
}
/**
* Add a recipient (To:).
*
* @param string $email E-mail address
* @param string $name Name (optional)
* @return bool
*/
public function addTo($email, $name = null)
{
try
{
$this->message->addTo($email, $name);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Add a recipient (CC:).
*
* @param string $email E-mail address
* @param string $name Name (optional)
* @return bool
*/
public function addCc($email, $name = null)
{
try
{
$this->message->addCc($email, $name);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Add a recipient (BCC:).
*
* @param string $email E-mail address
* @param string $name Name (optional)
* @return bool
*/
public function addBcc($email, $name = null)
{
try
{
$this->message->addBcc($email, $name);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Get the list of recipients.
*
* @return array();
*/
public function getRecipients()
{
$result = array();
foreach ($this->formatAddresses($this->message->getTo()) as $address)
{
$result[] = $address;
}
foreach ($this->formatAddresses($this->message->getCc()) as $address)
{
$result[] = $address;
}
foreach ($this->formatAddresses($this->message->getBcc()) as $address)
{
$result[] = $address;
}
return array_unique($result);
}
/**
* Set the Reply-To: address.
*
* @param string $replyTo
* @return bool
*/
public function setReplyTo($replyTo)
{
try
{
$this->message->setReplyTo(array($replyTo));
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Set the Return-Path: address.
*
* @param string $returnPath
* @return bool
*/
public function setReturnPath($returnPath)
{
try
{
$this->message->setReturnPath($returnPath);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Set the Message ID.
*
* @param string $messageId
* @return bool
*/
public function setMessageID($messageId)
{
try
{
$headers = $this->message->getHeaders();
$headers->get('Message-ID')->setId($messageId);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Set the In-Reply-To: header.
*
* @param string $inReplyTo
* @return bool
*/
public function setInReplyTo($inReplyTo)
{
try
{
$headers = $this->message->getHeaders();
$headers->addTextHeader('In-Reply-To', $inReplyTo);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Set the References: header.
*
* @param string $references
* @return bool
*/
public function setReferences($references)
{
try
{
$headers = $this->message->getHeaders();
$headers->addTextHeader('References', $references);
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Set the subject.
*
* @param string $subject
* @return bool
*/
public function setSubject($subject)
{
try
{
$this->message->setSubject(strval($subject));
return true;
}
catch (\Exception $e)
{
$this->errors[] = array($e->getMessage());
return false;
}
}
/**
* Get the subject.
*
* @return string
*/
public function getSubject()
{
return $this->message->getSubject();
}
/**
* Set the subject (alias to setSubject).
*
* @param string $subject
* @return bool
*/
public function setTitle($subject)
{
return $this->setSubject($subject);
}
/**
* Get the subject (alias to getSubject).
*
* @return string
*/
public function getTitle()
{
return $this->getSubject();
}
/**
* Set the body content.
*
* @param string $content
* @param string $content_type (optional)
* @return void
*/
public function setBody($content, $content_type = null)
{
if ($content_type !== null)
{
$this->setContentType($content_type);
}
if (strpos($this->content_type, 'html') !== false)
{
$content = preg_replace_callback('/<img([^>]+)>/i', array($this, 'convertImageURLs'), $content);
}
$this->message->setBody($content, $this->content_type);
}
/**
* Get the body content.
*
* @return string
*/
public function getBody()
{
return $this->message->getBody();
}
/**
* Set the body content (alias to setBody).
*
* @param string $content
* @param string $content_type (optional)
* @return void
*/
public function setContent($content, $content_type = null)
{
return $this->setBody($content, $content_type);
}
/**
* Get the body content (alias to getBody).
*
* @return string
*/
public function getContent()
{
return $this->getBody();
}
/**
* Set the content type.
*
* @param string $mode The type
* @return void
*/
public function setContentType($type = 'text/html')
{
$this->content_type = (strpos($type, 'html') !== false) ? 'text/html' : ((strpos($type, '/') !== false) ? $type : 'text/plain');
}
/**
* Get the content type.
*
* @return string
*/
public function getContentType()
{
return $this->content_type;
}
/**
* Attach a file.
*
* @param string $local_filename
* @param string $display_filename (optional)
* @return bool
*/
public function attach($local_filename, $display_filename = null)
{
if ($display_filename === null)
{
$display_filename = basename($local_filename);
}
if (!Storage::exists($local_filename))
{
return false;
}
$attachment = \Swift_Attachment::fromPath($local_filename);
$attachment->setFilename($display_filename);
$result = $this->message->attach($attachment);
if ($result)
{
$this->attachments[] = (object)array(
'type' => 'attach',
'local_filename' => $local_filename,
'display_filename' => $display_filename,
'cid' => null,
);
return true;
}
else
{
return false;
}
}
/**
* Embed a file.
*
* @param string $local_filename
* @param string $cid (optional)
* @return string|false
*/
public function embed($local_filename, $cid = null)
{
if (!Storage::exists($local_filename))
{
return false;
}
$embedded = \Swift_EmbeddedFile::fromPath($local_filename);
if ($cid !== null)
{
$embedded->setId(preg_replace('/^cid:/i', '', $cid));
}
$result = $this->message->embed($embedded);
if ($result)
{
$this->attachments[] = (object)array(
'type' => 'embed',
'local_filename' => $local_filename,
'display_filename' => null,
'cid' => $result,
);
return $result;
}
else
{
return false;
}
}
/**
* Get the list of attachments to this message.
*
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* Send the email.
*
* @return bool
*/
public function send()
{
try
{
return $this->driver->send($this);
}
catch(\Exception $e)
{
$this->errors[] = $e->getMessage();
return false;
}
}
/**
* Convert image paths to absolute URLs.
*
* @see Mail::setContent()
* @param array $matches Match info.
* @return string
*/
protected function convertImageURLs(array $matches)
{
return preg_replace('/src=(["\']?)files/i', 'src=$1' . URL::getCurrentDomainURL(\RX_BASEURL) . 'files', $matches[0]);
}
/**
* Format an array of addresses for display.
*
* @param array $addresses
* @return array
*/
protected function formatAddresses(array $addresses)
{
$result = array();
foreach($address as $email => $name)
{
if(strval($name) === '')
{
$result[] = $email;
}
else
{
$result[] = $name . ' <' . $email . '>';
}
}
return $result;
}
}