api = new API($apiKey);
$this->sender = $sender;
$this->replyTo = $replyTo;
$this->servicesChecker = new ServicesChecker();
$this->errorMapper = $errorMapper;
$this->bridge = $bridge;
$this->authorizedEmailsController = $authorizedEmailsController;
$this->blacklist = new BlacklistCheck();
$this->url = $url;
}
public function send($newsletter, $subscriber, $extraParams = []): array {
if ($this->servicesChecker->isMailPoetAPIKeyValid() === false) {
return Mailer::formatMailerErrorResult($this->errorMapper->getInvalidApiKeyError());
}
$subscribersForBlacklistCheck = is_array($subscriber) ? $subscriber : [$subscriber];
foreach ($subscribersForBlacklistCheck as $sub) {
if ($this->blacklist->isBlacklisted($sub)) {
$error = $this->errorMapper->getBlacklistError($sub);
return Mailer::formatMailerErrorResult($error);
}
}
$messageBody = $this->getBody($newsletter, $subscriber, $extraParams);
$result = $this->api->sendMessages($messageBody);
switch ($result['status']) {
case API::SENDING_STATUS_CONNECTION_ERROR:
$error = $this->errorMapper->getConnectionError($result['message']);
return Mailer::formatMailerErrorResult($error);
case API::SENDING_STATUS_SEND_ERROR:
$error = $this->processSendError($result, $subscriber, $newsletter);
return Mailer::formatMailerErrorResult($error);
case API::RESPONSE_STATUS_OK:
default:
return Mailer::formatMailerSendSuccessResult();
}
}
public function processSendError($result, $subscriber, $newsletter) {
if (empty($result['code'])) {
return $this->errorMapper->getErrorForResult($result, $subscriber, $this->sender, $newsletter);
}
switch ($result['code']) {
case API::RESPONSE_CODE_KEY_INVALID:
$this->bridge->invalidateMssKey();
break;
case API::RESPONSE_CODE_CAN_NOT_SEND:
if ($result['error'] === API::ERROR_MESSAGE_INVALID_FROM) {
$this->authorizedEmailsController->checkAuthorizedEmailAddresses();
}
break;
case API::RESPONSE_CODE_PAYLOAD_ERROR:
if (!empty($result['error']) && $result['error'] === API::ERROR_MESSAGE_BULK_EMAIL_FORBIDDEN) {
$this->authorizedEmailsController->checkAuthorizedEmailAddresses();
}
break;
}
return $this->errorMapper->getErrorForResult($result, $subscriber, $this->sender, $newsletter);
}
public function processSubscriber($subscriber) {
preg_match('!(?P