Skip to content

Instantly share code, notes, and snippets.

@webnitros
Created May 6, 2019 11:22
Show Gist options
  • Select an option

  • Save webnitros/c06abd88d799bf814460e507cd5b2735 to your computer and use it in GitHub Desktop.

Select an option

Save webnitros/c06abd88d799bf814460e507cd5b2735 to your computer and use it in GitHub Desktop.
<?php
interface bxMailingInterface
{
/**
* Send message queue to service
*
* @return array|boolean $response
*/
public function initialize();
/**
* Send message queue to service
*
*
* @return boolean $response
*/
public function send();
/**
* Receives message queue
*
* @return boolean $response
*/
public function state();
/**
* Return content message queue
*
* @return boolean $response
*/
public function content();
}
class bxMailingHandler implements bxMailingInterface
{
/* @var modX $modx */
public $modx;
/* @var bxSender $bxs */
public $bxs;
/* @var \modHelpers\Mandrill $Mandrill */
protected $Mandrill;
/* @var bxQueue $queue */
protected $queue;
/**
* @param bxQueue $object
* @param array $config
*/
function __construct(bxQueue & $object, array $config = array())
{
$this->queue = &$object;
$this->bxs = &$object->bxs;
$this->modx = &$object->xpdo;
}
/**
* Инициализация процессов
* @return bool
*/
public function initialize()
{
return true;
}
/** @inheritdoc} */
public function send()
{
/* @var modPHPMailer $mail */
$mail = $this->modx->getService('mail', 'mail.modPHPMailer');
$queue = $this->queue;
if ($MailSender = $queue->loadSubscriber()->loadNewsletter()->loadMailSender()) {
$MailSender->transporter($mail);
} else {
$name = $reply_to = $from = $this->modx->getOption('emailsender');
$mail->set(modMail::MAIL_FROM, $from);
$mail->set(modMail::MAIL_FROM_NAME, $name);
$mail->address('reply-to', $reply_to);
}
$mail->address('to', $queue->get('email_to'));
$mail->set(modMail::MAIL_BODY, $queue->get('email_body'));
$mail->set(modMail::MAIL_SUBJECT, $queue->get('email_subject'));
$mail->setHTML(true);
/* @var PHPMailer $mailer */
$mailer = $mail->mailer;
// Добавляем свой хешь в письмо, чтобы можно было отследить наше сообщение в случае если произойдет ошибка
$mailer->addCustomHeader('bxSendex-hash', $queue->get('hash'));
if ($ReturnPath = $queue->loadSubscriber()->loadNewsletter()->loadReturnPath()) {
// Установка Return Path только в случае если не используется SMTP адрес
if (!$MailSender->isSMTP()) {
$mail->set(modMail::MAIL_SENDER, $ReturnPath->get('email'));
}
}
if (!$mail->send()) {
$this->modx->log(xPDO::LOG_LEVEL_ERROR, 'An error occurred while trying to send the email: ' . $mail->mailer->ErrorInfo);
$state = 'error';
} else {
$state = 'sent';
}
if ($state == 'error') {
echo '<pre>';
print_r($mail->mailer->ErrorInfo);
die;
}
$mail->reset();
$queue->operation('update', array(
'state' => $state,
'reject_reason' => $state,
'service_message' => array('error' => $mail->mailer->ErrorInfo),
));
return true;
}
/** @inheritdoc} */
public function state()
{
$hash_service = $this->queue->get('hash_service');
$response = $this->Mandrill->getInfo($hash_service);
$this->queue->operation('update', array(
'state' => $response['state'],
'reject_reason' => $response['state'],
'opens' => $response['opens'],
'clicks' => $response['clicks'],
'service_message' => $response
));
return true;
}
/** @inheritdoc} */
public function content()
{
return $this->queue->get('email_body');
}
/** @inheritdoc} */
public function resubmit()
{
return $this->send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment