Show error notice when email is sent from unauthorized email

[MAILPOET-1787]
This commit is contained in:
Ján Mikláš
2019-02-19 16:45:47 +01:00
committed by M. Shull
parent 79bd178123
commit f5feb032fe
8 changed files with 56 additions and 10 deletions

View File

@ -391,7 +391,11 @@ const StatisticsMixin = {
const MailerMixin = {
checkMailerStatus: function checkMailerStatus(state) {
if (state.meta.mta_log.error && state.meta.mta_log.status === 'paused') {
if (
state.meta.mta_log.error
&& state.meta.mta_log.status === 'paused'
&& state.meta.mta_log.error.operation !== 'authorization'
) {
const errorType = MailerMixin.getMailerErrorType(state);
MailPoet.Notice[errorType](
'',

View File

@ -73,6 +73,7 @@ class Menu {
$this->subscribers_over_limit = $subscribers_feature->check();
$this->checkMailPoetAPIKey();
$this->checkPremiumKey();
$this->checkFromEmailAuthorization();
add_action(
'admin_menu',
@ -836,6 +837,13 @@ class Menu {
$this->premium_key_valid = $checker->isPremiumKeyValid($show_notices);
}
function checkFromEmailAuthorization(ServicesChecker $checker = null) {
if (self::isOnMailPoetAdminPage()) {
$checker = $checker ?: new ServicesChecker();
$checker->isFromEmailAuthorized(true);
}
}
function getLimitPerPage($model = null) {
if ($model === null) {
return Listing\Handler::DEFAULT_LIMIT_PER_PAGE;

View File

@ -1,6 +1,7 @@
<?php
namespace MailPoet\Config;
use MailPoet\Mailer\MailerError;
use MailPoet\Models\Subscriber;
use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController;
@ -115,4 +116,17 @@ class ServicesChecker {
return false;
}
function isFromEmailAuthorized($display_error_notice = true) {
$mta_log_error = $this->settings->get('mta_log.error', []);
if ($mta_log_error && $mta_log_error['operation'] === MailerError::OPERATION_AUTHORIZATION) {
if ($display_error_notice) {
WPNotice::displayError($mta_log_error['error_message'], 'js-error-unauthorized-email', '', false, false);
}
return false;
}
return true;
}
}

View File

@ -4,6 +4,7 @@ namespace MailPoet\Mailer;
class MailerError {
const OPERATION_CONNECT = 'connect';
const OPERATION_SEND = 'send';
const OPERATION_AUTHORIZATION = 'authorization';
const LEVEL_HARD = 'hard';
const LEVEL_SOFT = 'soft';

View File

@ -22,8 +22,9 @@ class MailPoetMapper {
);
}
function getErrorForResult(array $result, $subscribers) {
function getErrorForResult(array $result, $subscribers, $sender = null) {
$level = MailerError::LEVEL_HARD;
$operation = MailerError::OPERATION_SEND;
$retry_interval = null;
$subscribers_errors = [];
$result_code = !empty($result['code']) ? $result['code'] : null;
@ -51,18 +52,35 @@ class MailPoetMapper {
$retry_interval = self::TEMPORARY_UNAVAILABLE_RETRY_INTERVAL;
break;
case API::RESPONSE_CODE_CAN_NOT_SEND:
$message = Helpers::replaceLinkTags(
__('You currently are not permitted to send any emails with MailPoet Sending Service, which may have happened due to poor deliverability. Please [link]contact our support team[/link] to resolve the issue.', 'mailpoet'),
'https://www.mailpoet.com/support/',
array('target' => '_blank')
);
if ($result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED) {
$operation = MailerError::OPERATION_AUTHORIZATION;
$message = sprintf(__('<p>The MailPoet Sending Service did not send your latest email because the address <i>%s</i> is not yet authorized.</p>', 'mailpoet'), $sender ? $sender['from_email'] : __('Unknown address'));
$message .= '<p>';
$message .= Helpers::replaceLinkTags(
__('[link]Authorize your email in your account now.[/link]', 'mailpoet'),
'https://account.mailpoet.com/account/authorization',
array(
'class' => 'button button-primary',
'target' => '_blank',
)
);
$message .= ' &nbsp; <button class="button js-button-resume-sending">' . __('Resume sending', 'mailpoet') . '</button>';
$message .= '</p>';
$message .= "<script>jQuery('.js-button-resume-sending').on('click', function() { MailPoet.Ajax.post({ api_version: window.mailpoet_api_version, endpoint: 'mailer', action: 'resumeSending' }).done(function() { jQuery('.js-error-unauthorized-email').slideUp(); MailPoet.Notice.success(MailPoet.I18n.t('mailerSendingResumedNotice')); if (window.mailpoet_listing) { window.mailpoet_listing.forceUpdate(); }}).fail(function(response) { if (response.errors.length > 0) { MailPoet.Notice.error(response.errors.map(function(error) { return error.message }), { scroll: true }); }}); })</script>";
} else {
$message = Helpers::replaceLinkTags(
__('You currently are not permitted to send any emails with MailPoet Sending Service, which may have happened due to poor deliverability. Please [link]contact our support team[/link] to resolve the issue.', 'mailpoet'),
'https://www.mailpoet.com/support/',
array('target' => '_blank')
);
}
break;
case API::RESPONSE_CODE_KEY_INVALID:
case API::RESPONSE_CODE_PAYLOAD_TOO_BIG:
default:
$message = $result['message'];
}
return new MailerError(MailerError::OPERATION_SEND, $level, $message, $retry_interval, $subscribers_errors);
return new MailerError($operation, $level, $message, $retry_interval, $subscribers_errors);
}
private function getSubscribersErrors($result_parsed, $subscribers) {

View File

@ -51,7 +51,7 @@ class MailPoet {
if (!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
Bridge::invalidateKey();
}
return $this->error_mapper->getErrorForResult($result, $subscriber);
return $this->error_mapper->getErrorForResult($result, $subscriber, $this->sender);
}
function processSubscriber($subscriber) {

View File

@ -12,6 +12,7 @@ class Notice {
private $message;
private $classes;
private $data_notice_name;
private $render_in_paragraph;
function __construct($type, $message, $classes = '', $data_notice_name = '', $render_in_paragraph = true) {
$this->type = $type;

View File

@ -68,7 +68,7 @@ class MailPoetMapperTest extends \MailPoetTest {
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
expect($error)->isInstanceOf(MailerError::class);
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
expect($error->getOperation())->equals(MailerError::OPERATION_AUTHORIZATION);
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
expect($error->getMessage())->contains('The MailPoet Sending Service did not send your latest email because the address');
}