mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-22 05:15:29 +09:00
Merge pull request #460 from kijin/pr/advanced-mailer
고급 메일 발송 모듈의 기능을 라이믹스에 포함
This commit is contained in:
commit
9a7445b677
141 changed files with 5770 additions and 683 deletions
113
common/framework/drivers/mail/base.php
Normal file
113
common/framework/drivers/mail/base.php
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
<?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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of API types supported by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAPITypes()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
34
common/framework/drivers/mail/dummy.php
Normal file
34
common/framework/drivers/mail/dummy.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The dummy mail driver.
|
||||
*/
|
||||
class Dummy extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
76
common/framework/drivers/mail/mailfunction.php
Normal file
76
common/framework/drivers/mail/mailfunction.php
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The mail() function mail driver.
|
||||
*/
|
||||
class MailFunction extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$this->mailer = \Swift_Mailer::newInstance(\Swift_MailTransport::newInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this mail driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName()
|
||||
{
|
||||
return 'PHP mail()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'ip4:$SERVER_ADDR';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
137
common/framework/drivers/mail/mailgun.php
Normal file
137
common/framework/drivers/mail/mailgun.php
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The Mailgun mail driver.
|
||||
*/
|
||||
class Mailgun extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* The API URL.
|
||||
*/
|
||||
protected static $_url = 'https://api.mailgun.net/v3';
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_domain', 'api_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'include:mailgun.org';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return 'mailo._domainkey';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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[] = $address;
|
||||
}
|
||||
}
|
||||
if ($cc = $message->message->getCc())
|
||||
{
|
||||
foreach($cc as $address => $name)
|
||||
{
|
||||
$recipients[] = $address;
|
||||
}
|
||||
}
|
||||
if ($bcc = $message->message->getBcc())
|
||||
{
|
||||
foreach($bcc as $address => $name)
|
||||
{
|
||||
$recipients[] = $address;
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare data and options for Requests.
|
||||
$boundary = str_repeat('-', 24) . substr(md5(mt_rand()), 0, 16);
|
||||
$headers = array(
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
|
||||
);
|
||||
$data = implode("\r\n", array(
|
||||
'--' . $boundary,
|
||||
'Content-Disposition: form-data; name="to"',
|
||||
'',
|
||||
implode(', ', $recipients),
|
||||
'--' . $boundary,
|
||||
'Content-Disposition: attachment; name="message"; filename="message.eml"',
|
||||
'Content-Type: message/rfc822',
|
||||
'Content-Transfer-Encoding: binary',
|
||||
'',
|
||||
$message->message->toString(),
|
||||
'--' . $boundary . '--',
|
||||
'',
|
||||
));
|
||||
$options = array(
|
||||
'auth' => array('api', $this->_config['api_token']),
|
||||
'timeout' => 5,
|
||||
'useragent' => 'PHP',
|
||||
);
|
||||
|
||||
// Send the API request.
|
||||
$url = self::$_url . '/' . $this->_config['api_domain'] . '/messages.mime';
|
||||
$request = \Requests::post($url, $headers, $data, $options);
|
||||
$result = @json_decode($request->body);
|
||||
|
||||
// Parse the result.
|
||||
if (!$result)
|
||||
{
|
||||
$message->errors[] = 'Mailgun: Connection error: ' . $request->body;
|
||||
return false;
|
||||
}
|
||||
elseif (!$result->id)
|
||||
{
|
||||
$message->errors[] = 'Mailgun: ' . $result->message;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
common/framework/drivers/mail/mandrill.php
Normal file
52
common/framework/drivers/mail/mandrill.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The Mandrill mail driver.
|
||||
*/
|
||||
class Mandrill extends SMTP implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct(array $config)
|
||||
{
|
||||
$config['smtp_host'] = 'smtp.mandrillapp.com';
|
||||
$config['smtp_port'] = 465;
|
||||
$config['smtp_security'] = 'ssl';
|
||||
$config['smtp_user'] = $config['api_user'];
|
||||
$config['smtp_pass'] = $config['api_token'];
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_user', 'api_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'include:spf.mandrillapp.com';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return 'mandrill._domainkey';
|
||||
}
|
||||
}
|
||||
52
common/framework/drivers/mail/postmark.php
Normal file
52
common/framework/drivers/mail/postmark.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The Postmark mail driver.
|
||||
*/
|
||||
class Postmark extends SMTP implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct(array $config)
|
||||
{
|
||||
$config['smtp_host'] = 'smtp.postmarkapp.com';
|
||||
$config['smtp_port'] = 587;
|
||||
$config['smtp_security'] = 'tls';
|
||||
$config['smtp_user'] = $config['api_token'];
|
||||
$config['smtp_pass'] = $config['api_token'];
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'include:spf.mtasv.net';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return '********.pm._domainkey';
|
||||
}
|
||||
}
|
||||
52
common/framework/drivers/mail/sendgrid.php
Normal file
52
common/framework/drivers/mail/sendgrid.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The SendGrid mail driver.
|
||||
*/
|
||||
class SendGrid extends SMTP implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct(array $config)
|
||||
{
|
||||
$config['smtp_host'] = 'smtp.sendgrid.net';
|
||||
$config['smtp_port'] = 465;
|
||||
$config['smtp_security'] = 'ssl';
|
||||
$config['smtp_user'] = $config['api_user'];
|
||||
$config['smtp_pass'] = $config['api_pass'];
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_user', 'api_pass');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'include:sendgrid.net';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return 'smtpapi._domainkey';
|
||||
}
|
||||
}
|
||||
120
common/framework/drivers/mail/ses.php
Normal file
120
common/framework/drivers/mail/ses.php
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
<?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['api_user'], $config['api_pass']);
|
||||
$transport->setDebug(array($this, 'debugCallback'));
|
||||
$transport->setEndpoint('https://email.' . strtolower($config['api_type']) . '.amazonaws.com/');
|
||||
$this->mailer = \Swift_Mailer::newInstance($transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_user', 'api_pass', 'api_type');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of API types supported by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAPITypes()
|
||||
{
|
||||
return array('us-east-1', 'us-west-2', 'eu-west-1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return '********._domainkey';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
74
common/framework/drivers/mail/smtp.php
Normal file
74
common/framework/drivers/mail/smtp.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The SMTP mail driver.
|
||||
*/
|
||||
class SMTP extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct(array $config)
|
||||
{
|
||||
$transport = \Swift_SmtpTransport::newInstance($config['smtp_host'], $config['smtp_port'], $config['smtp_security']);
|
||||
$transport->setUsername($config['smtp_user']);
|
||||
$transport->setPassword($config['smtp_pass']);
|
||||
$local_domain = $transport->getLocalDomain();
|
||||
if (preg_match('/^\*\.(.+)$/', $local_domain, $matches))
|
||||
{
|
||||
$transport->setLocalDomain($matches[1]);
|
||||
}
|
||||
$this->mailer = \Swift_Mailer::newInstance($transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('smtp_host', 'smtp_port', 'smtp_security', 'smtp_user', 'smtp_pass');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
try
|
||||
{
|
||||
$result = $this->mailer->send($message->message, $errors);
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$message->errors[] = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($errors as $error)
|
||||
{
|
||||
$message->errors[] = 'Failed to send to ' . $error;
|
||||
}
|
||||
return (bool)$result;
|
||||
}
|
||||
}
|
||||
137
common/framework/drivers/mail/sparkpost.php
Normal file
137
common/framework/drivers/mail/sparkpost.php
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The SparkPost mail driver.
|
||||
*/
|
||||
class SparkPost extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* The API URL.
|
||||
*/
|
||||
protected static $_url = 'https://api.sparkpost.com/api/v1/transmissions';
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'include:sparkpostmail.com';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return '********._domainkey';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
199
common/framework/drivers/mail/woorimail.php
Normal file
199
common/framework/drivers/mail/woorimail.php
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Mail;
|
||||
|
||||
/**
|
||||
* The Woorimail mail driver.
|
||||
*/
|
||||
class Woorimail extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
||||
{
|
||||
/**
|
||||
* 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' => '전용채널에 도메인이 등록되어 있지 않습니다.',
|
||||
);
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig()
|
||||
{
|
||||
return array('api_domain', 'api_token', 'api_type');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of API types supported by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAPITypes()
|
||||
{
|
||||
return array('free', 'paid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint()
|
||||
{
|
||||
return 'include:woorimail.com';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
71
common/framework/drivers/mailinterface.php
Normal file
71
common/framework/drivers/mailinterface.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?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);
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this mail driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName();
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig();
|
||||
|
||||
/**
|
||||
* Get the list of API types supported by this mail driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAPITypes();
|
||||
|
||||
/**
|
||||
* Get the SPF hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getSPFHint();
|
||||
|
||||
/**
|
||||
* Get the DKIM hint.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDKIMHint();
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
101
common/framework/helpers/confighelper.php
Normal file
101
common/framework/helpers/confighelper.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Helpers;
|
||||
|
||||
use Rhymix\Framework\Config;
|
||||
use Rhymix\Framework\Plugin;
|
||||
|
||||
/**
|
||||
* Config helper class.
|
||||
*/
|
||||
class ConfigHelper
|
||||
{
|
||||
/**
|
||||
* Cache plugin configuration during consolidation.
|
||||
*/
|
||||
protected static $_config_cache = array();
|
||||
|
||||
/**
|
||||
* Consolidate configuration from multiple sources.
|
||||
*
|
||||
* @param array $format
|
||||
* @return array
|
||||
*/
|
||||
public static function consolidate($format)
|
||||
{
|
||||
self::$_config_cache = array();
|
||||
$result = array();
|
||||
|
||||
foreach ($format as $key => $value)
|
||||
{
|
||||
$result[$key] = self::_parseConfigValue((array)$value);
|
||||
}
|
||||
|
||||
self::$_config_cache = array();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and get a configuration value.
|
||||
*
|
||||
* @param array $value
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function _parseConfigValue(array $value)
|
||||
{
|
||||
$filters = array();
|
||||
$result = null;
|
||||
|
||||
foreach ($value as $option)
|
||||
{
|
||||
$option = array_map('trim', explode(':', $option, 2));
|
||||
if (count($option) === 1)
|
||||
{
|
||||
if (function_exists($option[0]))
|
||||
{
|
||||
$filters[] = $option[0];
|
||||
}
|
||||
}
|
||||
elseif ($result !== null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
elseif ($option[0] === 'common')
|
||||
{
|
||||
$result = Config::get($option[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isset(self::$_config_cache[$option[0]]))
|
||||
{
|
||||
self::$_config_cache[$option[0]] = getModel('module')->getModuleConfig($option[0]) ?: new stdClass;
|
||||
}
|
||||
$options = explode('.', $option[1]);
|
||||
$temp = self::$_config_cache[$option[0]];
|
||||
foreach ($options as $step)
|
||||
{
|
||||
if (is_object($temp) && isset($temp->$step))
|
||||
{
|
||||
$temp = $temp->$step;
|
||||
}
|
||||
elseif (is_array($temp) && isset($temp[$step]))
|
||||
{
|
||||
$temp = $temp[$step];
|
||||
}
|
||||
else
|
||||
{
|
||||
$temp = null;
|
||||
}
|
||||
}
|
||||
$result = $temp;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($filters as $filter)
|
||||
{
|
||||
$result = $filter($result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
670
common/framework/mail.php
Normal file
670
common/framework/mail.php
Normal file
|
|
@ -0,0 +1,670 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
/**
|
||||
* The mail class.
|
||||
*/
|
||||
class Mail
|
||||
{
|
||||
/**
|
||||
* Instance properties.
|
||||
*/
|
||||
public $message = null;
|
||||
public $driver = null;
|
||||
public $caller = '';
|
||||
protected $content_type = 'text/html';
|
||||
protected $attachments = array();
|
||||
public $errors = array();
|
||||
protected $sent = false;
|
||||
|
||||
/**
|
||||
* Static properties.
|
||||
*/
|
||||
public static $default_driver = null;
|
||||
public static $custom_drivers = array();
|
||||
|
||||
/**
|
||||
* Set the default driver.
|
||||
*
|
||||
* @param object $driver
|
||||
* @return void
|
||||
*/
|
||||
public static function setDefaultDriver(Drivers\MailInterface $driver)
|
||||
{
|
||||
self::$default_driver = $driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default driver.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function getDefaultDriver()
|
||||
{
|
||||
if (!self::$default_driver)
|
||||
{
|
||||
$default_driver = config('mail.type');
|
||||
$default_driver_class = '\\Rhymix\\Framework\\Drivers\Mail\\' . $default_driver;
|
||||
if (class_exists($default_driver_class))
|
||||
{
|
||||
$default_driver_config = config('mail.' . $default_driver) ?: array();
|
||||
self::$default_driver = $default_driver_class::getInstance($default_driver_config);
|
||||
}
|
||||
else
|
||||
{
|
||||
self::$default_driver = Drivers\Mail\MailFunction::getInstance(array());
|
||||
}
|
||||
}
|
||||
return self::$default_driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom mail driver.
|
||||
*/
|
||||
public static function addDriver(Drivers\MailInterface $driver)
|
||||
{
|
||||
self::$custom_drivers[] = $driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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] = array(
|
||||
'name' => $class_name::getName(),
|
||||
'required' => $class_name::getRequiredConfig(),
|
||||
'api_types' => $class_name::getAPITypes(),
|
||||
'spf_hint' => $class_name::getSPFHint(),
|
||||
'dkim_hint' => $class_name::getDKIMHint(),
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach (self::$custom_drivers as $driver)
|
||||
{
|
||||
if ($driver->isSupported())
|
||||
{
|
||||
$result[strtolower(class_basename($driver))] = array(
|
||||
'name' => $driver->getName(),
|
||||
'required' => $driver->getRequiredConfig(),
|
||||
'api_types' => $driver->getAPITypes(),
|
||||
'spf_hint' => $class_name::getSPFHint(),
|
||||
'dkim_hint' => $class_name::getDKIMHint(),
|
||||
);
|
||||
}
|
||||
}
|
||||
ksort($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->message = \Swift_Message::newInstance();
|
||||
$this->driver = self::getDefaultDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->message->getFrom();
|
||||
return $list ? array_first($this->formatAddresses($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()
|
||||
{
|
||||
// Get caller information.
|
||||
$backtrace = debug_backtrace(0);
|
||||
if(count($backtrace) && isset($backtrace[0]['file']))
|
||||
{
|
||||
$this->caller = $backtrace[0]['file'] . ($backtrace[0]['line'] ? (' line ' . $backtrace[0]['line']) : '');
|
||||
}
|
||||
|
||||
// Reset Message-ID in case send() is called multiple times.
|
||||
$random = substr(hash('sha256', mt_rand() . microtime() . getmypid()), 0, 32);
|
||||
$sender = $this->message->getFrom(); reset($sender);
|
||||
$id = $random . '@' . (preg_match('/^(.+)@([^@]+)$/', key($sender), $matches) ? $matches[2] : 'swift.generated');
|
||||
$this->message->getHeaders()->get('Message-ID')->setId($id);
|
||||
|
||||
$output = \ModuleHandler::triggerCall('mail.send', 'before', $this);
|
||||
if(!$output->toBool())
|
||||
{
|
||||
$this->errors[] = $output->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$this->sent = $this->driver->send($this) ? true : false;
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$this->errors[] = $e->getMessage();
|
||||
$this->sent = false;
|
||||
}
|
||||
|
||||
$output = \ModuleHandler::triggerCall('mail.send', 'after', $this);
|
||||
if(!$output->toBool())
|
||||
{
|
||||
$this->errors[] = $output->getMessage();
|
||||
}
|
||||
|
||||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the message was sent.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSent()
|
||||
{
|
||||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get caller information.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCaller()
|
||||
{
|
||||
return $this->caller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get errors.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors()
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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($addresses)
|
||||
{
|
||||
$result = array();
|
||||
|
||||
if (!$addresses)
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
foreach($addresses as $email => $name)
|
||||
{
|
||||
if(strval($name) === '')
|
||||
{
|
||||
$result[] = $email;
|
||||
}
|
||||
else
|
||||
{
|
||||
$result[] = $name . ' <' . $email . '>';
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue