Nessuna descrizione

MailPoet.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace MailPoet\Mailer\Methods;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Config\ServicesChecker;
  5. use MailPoet\Mailer\Mailer;
  6. use MailPoet\Mailer\MailerError;
  7. use MailPoet\Mailer\Methods\Common\BlacklistCheck;
  8. use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
  9. use MailPoet\Services\AuthorizedEmailsController;
  10. use MailPoet\Services\Bridge;
  11. use MailPoet\Services\Bridge\API;
  12. class MailPoet {
  13. public $api;
  14. public $sender;
  15. public $replyTo;
  16. public $servicesChecker;
  17. /** @var AuthorizedEmailsController */
  18. private $authorizedEmailsController;
  19. /** @var MailPoetMapper */
  20. private $errorMapper;
  21. /** @var BlacklistCheck */
  22. private $blacklist;
  23. public function __construct(
  24. $apiKey,
  25. $sender,
  26. $replyTo,
  27. MailPoetMapper $errorMapper,
  28. AuthorizedEmailsController $authorizedEmailsController
  29. ) {
  30. $this->api = new API($apiKey);
  31. $this->sender = $sender;
  32. $this->replyTo = $replyTo;
  33. $this->servicesChecker = new ServicesChecker();
  34. $this->errorMapper = $errorMapper;
  35. $this->authorizedEmailsController = $authorizedEmailsController;
  36. $this->blacklist = new BlacklistCheck();
  37. }
  38. public function send($newsletter, $subscriber, $extraParams = []) {
  39. if ($this->servicesChecker->isMailPoetAPIKeyValid() === false) {
  40. return Mailer::formatMailerErrorResult($this->errorMapper->getInvalidApiKeyError());
  41. }
  42. $subscribersForBlacklistCheck = is_array($subscriber) ? $subscriber : [$subscriber];
  43. foreach ($subscribersForBlacklistCheck as $sub) {
  44. if ($this->blacklist->isBlacklisted($sub)) {
  45. $error = $this->errorMapper->getBlacklistError($sub);
  46. return Mailer::formatMailerErrorResult($error);
  47. }
  48. }
  49. $messageBody = $this->getBody($newsletter, $subscriber, $extraParams);
  50. $result = $this->api->sendMessages($messageBody);
  51. switch ($result['status']) {
  52. case API::SENDING_STATUS_CONNECTION_ERROR:
  53. $error = $this->errorMapper->getConnectionError($result['message']);
  54. return Mailer::formatMailerErrorResult($error);
  55. case API::SENDING_STATUS_SEND_ERROR:
  56. $error = $this->processSendError($result, $subscriber, $newsletter);
  57. return Mailer::formatMailerErrorResult($error);
  58. case API::SENDING_STATUS_OK:
  59. default:
  60. return Mailer::formatMailerSendSuccessResult();
  61. }
  62. }
  63. public function processSendError($result, $subscriber, $newsletter) {
  64. if (!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
  65. Bridge::invalidateKey();
  66. } elseif (!empty($result['code'])
  67. && $result['code'] === API::RESPONSE_CODE_CAN_NOT_SEND
  68. && $result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED
  69. ) {
  70. $this->authorizedEmailsController->checkAuthorizedEmailAddresses();
  71. }
  72. return $this->errorMapper->getErrorForResult($result, $subscriber, $this->sender, $newsletter);
  73. }
  74. public function processSubscriber($subscriber) {
  75. preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriberData);
  76. if (!isset($subscriberData['email'])) {
  77. $subscriberData = [
  78. 'email' => $subscriber,
  79. ];
  80. }
  81. return [
  82. 'email' => $subscriberData['email'],
  83. 'name' => (isset($subscriberData['name'])) ? $subscriberData['name'] : '',
  84. ];
  85. }
  86. public function getBody($newsletter, $subscriber, $extraParams = []) {
  87. if (is_array($newsletter) && is_array($subscriber)) {
  88. $body = [];
  89. for ($record = 0; $record < count($newsletter); $record++) {
  90. $body[] = $this->composeBody(
  91. $newsletter[$record],
  92. $this->processSubscriber($subscriber[$record]),
  93. (!empty($extraParams['unsubscribe_url'][$record])) ? $extraParams['unsubscribe_url'][$record] : false,
  94. (!empty($extraParams['meta'][$record])) ? $extraParams['meta'][$record] : false
  95. );
  96. }
  97. } else {
  98. $body[] = $this->composeBody(
  99. $newsletter,
  100. $this->processSubscriber($subscriber),
  101. (!empty($extraParams['unsubscribe_url'])) ? $extraParams['unsubscribe_url'] : false,
  102. (!empty($extraParams['meta'])) ? $extraParams['meta'] : false
  103. );
  104. }
  105. return $body;
  106. }
  107. private function composeBody($newsletter, $subscriber, $unsubscribeUrl, $meta): array {
  108. $body = [
  109. 'to' => ([
  110. 'address' => $subscriber['email'],
  111. 'name' => $subscriber['name'],
  112. ]),
  113. 'from' => ([
  114. 'address' => $this->sender['from_email'],
  115. 'name' => $this->sender['from_name'],
  116. ]),
  117. 'reply_to' => ([
  118. 'address' => $this->replyTo['reply_to_email'],
  119. ]),
  120. 'subject' => $newsletter['subject'],
  121. ];
  122. if (!empty($this->replyTo['reply_to_name'])) {
  123. $body['reply_to']['name'] = $this->replyTo['reply_to_name'];
  124. }
  125. if (!empty($newsletter['body']['html'])) {
  126. $body['html'] = $newsletter['body']['html'];
  127. }
  128. if (!empty($newsletter['body']['text'])) {
  129. $body['text'] = $newsletter['body']['text'];
  130. }
  131. if ($unsubscribeUrl) {
  132. $body['list_unsubscribe'] = $unsubscribeUrl;
  133. }
  134. if ($meta) {
  135. $body['meta'] = $meta;
  136. }
  137. return $body;
  138. }
  139. }