Reduce API calls to verify sender domain during sending

and call checkAuthorizedEmailAddresses on bulk email sending error.

[MAILPOET-5832]
This commit is contained in:
Brezo Cordero 2024-01-18 23:18:06 -06:00 committed by Aschepikov
parent 9d8de3b3cf
commit 4a0ed51c8c
4 changed files with 41 additions and 10 deletions

View File

@ -82,14 +82,20 @@ class MailPoet implements MailerMethod {
}
public function processSendError($result, $subscriber, $newsletter) {
if (!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
$this->bridge->invalidateMssKey();
} elseif (
!empty($result['code'])
&& $result['code'] === API::RESPONSE_CODE_CAN_NOT_SEND
&& $result['error'] === API::ERROR_MESSAGE_INVALID_FROM
) {
$this->authorizedEmailsController->checkAuthorizedEmailAddresses();
if (!empty($result['code'])) {
if ($result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
$this->bridge->invalidateMssKey();
} elseif (
($result['code'] === API::RESPONSE_CODE_CAN_NOT_SEND
&& $result['error'] === API::ERROR_MESSAGE_INVALID_FROM
)
||
($result['code'] === API::RESPONSE_CODE_PAYLOAD_ERROR
&& !empty($result['error']) && $result['error'] === API::ERROR_MESSAGE_BULK_EMAIL_FORBIDDEN
)
) {
$this->authorizedEmailsController->checkAuthorizedEmailAddresses();
}
}
return $this->errorMapper->getErrorForResult($result, $subscriber, $this->sender, $newsletter);
}

View File

@ -166,7 +166,10 @@ class AuthorizedEmailsController {
return true;
}
$verifiedDomains = $this->senderDomainController->getVerifiedSenderDomainsIgnoringCache();
$verifiedDomains = $context === 'activation' ?
$this->senderDomainController->getVerifiedSenderDomainsIgnoringCache() :
$this->senderDomainController->getVerifiedSenderDomains();
return $this->validateEmailDomainIsVerified($verifiedDomains, $newsletter->getSenderAddress());
}

View File

@ -333,6 +333,28 @@ class MailPoetAPITest extends \MailPoetTest {
$mailer->send([$this->newsletter], [$this->subscriber]);
}
public function testItCallsAuthorizedEmailsValidationOnBulkEmailRelatedError() {
$mailer = new MailPoet(
$this->settings['api_key'],
$this->sender,
$this->replyTo,
$this->diContainer->get(MailPoetMapper::class),
$this->makeEmpty(AuthorizedEmailsController::class, ['checkAuthorizedEmailAddresses' => Expected::once()]),
$this->diContainer->get(Bridge::class),
$this->diContainer->get(Url::class)
);
$mailer->api = $this->makeEmpty(
API::class,
['sendMessages' => [
'code' => API::RESPONSE_CODE_PAYLOAD_ERROR,
'status' => API::SENDING_STATUS_SEND_ERROR,
'message' => API::ERROR_MESSAGE_BULK_EMAIL_FORBIDDEN,
'error' => API::ERROR_MESSAGE_BULK_EMAIL_FORBIDDEN,
]]
);
$mailer->send([$this->newsletter], [$this->subscriber]);
}
public function testItChecksBlacklistBeforeSendingToASingleSubscriber() {
$blacklistedSubscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);

View File

@ -284,7 +284,7 @@ class AuthorizedEmailsControllerTest extends \MailPoetTest {
$verifiedDomains = ['email.com'];
$senderDomainMock = $this->make(AuthorizedSenderDomainController::class, [
'isAuthorizedDomainRequiredForExistingCampaigns' => Expected::once(true),
'getVerifiedSenderDomainsIgnoringCache' => Expected::once($verifiedDomains),
'getVerifiedSenderDomains' => Expected::once($verifiedDomains),
]);
$newsletter = new NewsletterEntity();