- Updates queue worker to use mailer router for sending

- Updates mailer router to detect method type
- Rebases master
This commit is contained in:
MrCasual
2015-12-03 19:12:18 -05:00
parent 4a2bbe3f88
commit acebf669a7
3 changed files with 54 additions and 85 deletions

View File

@ -9,17 +9,27 @@ if(!defined('ABSPATH')) exit;
class Mailer {
function __construct() {
list($this->fromName, $this->fromEmail) = $this->getSetting('sender');
list($this->fromName, $this->fromEmail, $this->fromNameEmail)
= $this->getSetting('sender');
$this->mailerType = array(
'AmazonSES' => 'API',
'ElasticEmail' => 'API',
'MailGun' => 'API',
'Mandrill' => 'API',
'SendGrid' => 'API',
'MailPoet' => null,
'SMTP' => null,
'WPMail' => null
);
$this->mailer = $this->getSetting('mailer');
$this->from = sprintf('%s <%s>', $this->fromName, $this->fromEmail);
}
function send($newsletter, $subscriber) {
$subscriber = $this->transformSubscriber($subscriber);
$mailer = $this->buildMailer();
return $mailer->send($newsletter, $subscriber);
return wp_send_json($mailer->send($newsletter, $subscriber));
}
function buildMailer() {
switch($this->mailer['method']) {
case 'AmazonSES':
@ -27,7 +37,7 @@ class Mailer {
$this->mailer['region'],
$this->mailer['access_key'],
$this->mailer['secret_key'],
$this->from
$this->fromNameEmail
);
break;
case 'ElasticEmail':
@ -40,7 +50,7 @@ class Mailer {
$mailer = new $this->mailer['class'](
$this->mailer['domain'],
$this->mailer['api_key'],
$this->from
$this->fromNameEmail
);
break;
case 'MailPoet':
@ -63,6 +73,12 @@ class Mailer {
$this->fromName
);
break;
case 'WPMail':
$mailer = new $this->mailer['class'](
$this->fromEmail,
$this->fromName
);
break;
case 'SMTP':
$mailer = new $this->mailer['class'](
$this->mailer['host'],
@ -70,12 +86,16 @@ class Mailer {
$this->mailer['authentication'],
$this->mailer['encryption'],
$this->fromEmail,
$this->fromName);
$this->fromName
);
break;
default:
throw new \Exception('Mailing method does not exist.');
break;
}
return $mailer;
}
function transformSubscriber($subscriber) {
if(!is_array($subscriber)) return $subscriber;
$first_name = (isset($subscriber['first_name'])) ? $subscriber['first_name'] : '';
@ -85,70 +105,27 @@ class Mailer {
$subscriber = trim(preg_replace('!\s\s+!', ' ', $subscriber));
return $subscriber;
}
function getSetting($setting) {
switch($setting) {
case 'mailer':
// TODO: remove
/* $mailers = array(
array(
'method' => 'AmazonSES',
'type' => 'API',
'access_key' => 'AKIAJM6Y5HMGXBLDNSRA',
'secret_key' => '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',
'region' => 'us-east-1'
),
array(
'method' => 'ElasticEmail',
'type' => 'API',
'api_key' => '997f1f7f-41de-4d7f-a8cb-86c8481370fa'
),
array(
'method' => 'MailGun',
'type' => 'API',
'api_key' => 'key-6cf5g5qjzenk-7nodj44gdt8phe6vam2',
'domain' => 'mrcasual.com'
),
array(
'method' => 'MailPoet',
'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU'
),
array(
'method' => 'Mandrill',
'type' => 'API',
'api_key' => '692ys1B7REEoZN7R-dYwNA'
),
array(
'method' => 'SendGrid',
'type' => 'API',
'api_key' => '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'
),
array(
'method' => 'SMTP',
'host' => 'email-smtp.us-west-2.amazonaws.com',
'port' => 587,
'authentication' => array(
'login' => 'AKIAIGPBLH6JWG5VCBQQ',
'password' => 'AudVHXHaYkvr54veCzqiqOxDiMMyfQW3/V6F1tYzGXY3'
),
'encryption' => 'tls'
),
array(
'method' => 'WPMail'
)
);*/
$mailer = Setting::getValue('mta', null);
if(!$mailer) throw new \Exception('Mailing method has not been configured.');
if(!$mailer || !isset($mailer['method'])) throw new \Exception('Mailing method is not configured.');
$mailer['class'] = 'MailPoet\\Mailer\\' .
((isset($mailer['type'])) ?
$mailer['type'] . '\\' . $mailer['method'] :
(($this->mailerType[$mailer['method']]) ?
$this->mailerType[$mailer['method']] . '\\' . $mailer['method'] :
$mailer['method']
);
return $mailer;
break;;
case 'sender':
$sender = Setting::getValue($setting, null);
return array($sender['name'], $sender['address']);
if(!$sender) throw new \Exception('Sender name and email are not configured.');
return array(
$sender['name'],
$sender['address'],
sprintf('%s <%s>', $sender['name'], $sender['address'])
);
break;
default:
return Setting::getValue($setting, null);