Add dummy driver and other transports supported by Advanced Mailer

This commit is contained in:
Kijin Sung 2016-05-15 19:56:28 +09:00
parent 0b20f3f5c7
commit ebb1b2fd4a
6 changed files with 248 additions and 1 deletions

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

View file

@ -0,0 +1,117 @@
<?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');
}
/**
* 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;
}
}
}

View file

@ -0,0 +1,32 @@
<?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');
}
}

View file

@ -0,0 +1,32 @@
<?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');
}
}

View file

@ -0,0 +1,32 @@
<?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');
}
}

View file

@ -67,7 +67,7 @@ class SMTP extends Base implements \Rhymix\Framework\Drivers\MailInterface
foreach ($errors as $error) foreach ($errors as $error)
{ {
$message->errors[] = $error; $message->errors[] = 'Failed to send to ' . $error;
} }
return (bool)$result; return (bool)$result;
} }