No Description

SMTP.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace MailPoet\Mailer\Methods;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Mailer\Mailer;
  5. use MailPoet\Mailer\Methods\Common\BlacklistCheck;
  6. use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. use MailPoetVendor\Swift_Mailer;
  9. use MailPoetVendor\Swift_Message;
  10. use MailPoetVendor\Swift_Plugins_LoggerPlugin;
  11. use MailPoetVendor\Swift_Plugins_Loggers_ArrayLogger;
  12. use MailPoetVendor\Swift_SmtpTransport;
  13. class SMTP {
  14. public $host;
  15. public $port;
  16. public $authentication;
  17. public $login;
  18. public $password;
  19. public $encryption;
  20. public $sender;
  21. public $replyTo;
  22. public $returnPath;
  23. public $mailer;
  24. private $mailerLogger;
  25. const SMTP_CONNECTION_TIMEOUT = 15; // seconds
  26. /** @var SMTPMapper */
  27. private $errorMapper;
  28. /** @var BlacklistCheck */
  29. private $blacklist;
  30. private $wp;
  31. public function __construct(
  32. $host,
  33. $port,
  34. $authentication,
  35. $encryption,
  36. $sender,
  37. $replyTo,
  38. $returnPath,
  39. SMTPMapper $errorMapper,
  40. $login = null,
  41. $password = null
  42. ) {
  43. $this->wp = new WPFunctions;
  44. $this->host = $host;
  45. $this->port = $port;
  46. $this->authentication = $authentication;
  47. $this->login = $login;
  48. $this->password = $password;
  49. $this->encryption = $encryption;
  50. $this->sender = $sender;
  51. $this->replyTo = $replyTo;
  52. $this->returnPath = ($returnPath) ?
  53. $returnPath :
  54. $this->sender['from_email'];
  55. $this->mailer = $this->buildMailer();
  56. $this->mailerLogger = new Swift_Plugins_Loggers_ArrayLogger();
  57. $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->mailerLogger));
  58. $this->errorMapper = $errorMapper;
  59. $this->blacklist = new BlacklistCheck();
  60. }
  61. public function send($newsletter, $subscriber, $extraParams = []) {
  62. if ($this->blacklist->isBlacklisted($subscriber)) {
  63. $error = $this->errorMapper->getBlacklistError($subscriber);
  64. return Mailer::formatMailerErrorResult($error);
  65. }
  66. try {
  67. $message = $this->createMessage($newsletter, $subscriber, $extraParams);
  68. $result = $this->mailer->send($message);
  69. } catch (\Exception $e) {
  70. return Mailer::formatMailerErrorResult(
  71. $this->errorMapper->getErrorFromException($e, $subscriber)
  72. );
  73. }
  74. if ($result === 1) {
  75. return Mailer::formatMailerSendSuccessResult();
  76. } else {
  77. $error = $this->errorMapper->getErrorFromLog($this->mailerLogger->dump(), $subscriber);
  78. return Mailer::formatMailerErrorResult($error);
  79. }
  80. }
  81. public function buildMailer() {
  82. $transport = new Swift_SmtpTransport($this->host, $this->port, $this->encryption);
  83. $connectionTimeout = $this->wp->applyFilters('mailpoet_mailer_smtp_connection_timeout', self::SMTP_CONNECTION_TIMEOUT);
  84. $transport->setTimeout($connectionTimeout);
  85. if ($this->authentication) {
  86. $transport
  87. ->setUsername($this->login)
  88. ->setPassword($this->password);
  89. }
  90. $transport = $this->wp->applyFilters('mailpoet_mailer_smtp_transport_agent', $transport);
  91. return new Swift_Mailer($transport);
  92. }
  93. public function createMessage($newsletter, $subscriber, $extraParams = []) {
  94. $message = (new Swift_Message())
  95. ->setTo($this->processSubscriber($subscriber))
  96. ->setFrom(
  97. [
  98. $this->sender['from_email'] => $this->sender['from_name'],
  99. ]
  100. )
  101. ->setSender($this->sender['from_email'])
  102. ->setReplyTo(
  103. [
  104. $this->replyTo['reply_to_email'] => $this->replyTo['reply_to_name'],
  105. ]
  106. )
  107. ->setReturnPath($this->returnPath)
  108. ->setSubject($newsletter['subject']);
  109. if (!empty($extraParams['unsubscribe_url'])) {
  110. $headers = $message->getHeaders();
  111. $headers->addTextHeader('List-Unsubscribe', '<' . $extraParams['unsubscribe_url'] . '>');
  112. }
  113. if (!empty($newsletter['body']['html'])) {
  114. $message = $message->setBody($newsletter['body']['html'], 'text/html');
  115. }
  116. if (!empty($newsletter['body']['text'])) {
  117. $message = $message->addPart($newsletter['body']['text'], 'text/plain');
  118. }
  119. return $message;
  120. }
  121. public function processSubscriber($subscriber) {
  122. preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
  123. if (!isset($subscriberData['email'])) {
  124. $subscriberData = [
  125. 'email' => $subscriber,
  126. ];
  127. }
  128. return [
  129. $subscriberData['email'] =>
  130. (isset($subscriberData['name'])) ? $subscriberData['name'] : '',
  131. ];
  132. }
  133. }