Add blacklist to Mailer [MAILPOET-2176]
This commit is contained in:
@ -8,6 +8,8 @@ use MailPoet\WP\Functions as WPFunctions;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class AmazonSES {
|
||||
use BlacklistTrait;
|
||||
|
||||
public $aws_access_key;
|
||||
public $aws_secret_key;
|
||||
public $aws_region;
|
||||
@ -67,6 +69,10 @@ class AmazonSES {
|
||||
}
|
||||
|
||||
function send($newsletter, $subscriber, $extra_params = []) {
|
||||
if ($this->isBlacklisted($subscriber)) {
|
||||
$error = $this->error_mapper->getBlacklistError($subscriber);
|
||||
return Mailer::formatMailerErrorResult($error);
|
||||
}
|
||||
try {
|
||||
$result = $this->wp->wpRemotePost(
|
||||
$this->url,
|
||||
|
33
lib/Mailer/Methods/BlacklistTrait.php
Normal file
33
lib/Mailer/Methods/BlacklistTrait.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods;
|
||||
|
||||
use MailPoet\Subscription\Blacklist;
|
||||
|
||||
trait BlacklistTrait {
|
||||
/** @var Blacklist */
|
||||
private $blacklist;
|
||||
|
||||
function isBlacklisted($subscriber) {
|
||||
$email = $this->getSubscriberEmailForBlacklistCheck($subscriber);
|
||||
return $this->getBlacklist()->isBlacklisted($email);
|
||||
}
|
||||
|
||||
function getSubscriberEmailForBlacklistCheck($subscriber) {
|
||||
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
|
||||
if (!isset($subscriber_data['email'])) {
|
||||
return $subscriber;
|
||||
}
|
||||
return $subscriber_data['email'];
|
||||
}
|
||||
|
||||
function getBlacklist() {
|
||||
if (!$this->blacklist instanceof Blacklist) {
|
||||
$this->blacklist = new Blacklist();
|
||||
}
|
||||
return $this->blacklist;
|
||||
}
|
||||
|
||||
function setBlacklist(Blacklist $blacklist) {
|
||||
$this->blacklist = $blacklist;
|
||||
}
|
||||
}
|
@ -7,8 +7,11 @@ use MailPoet\Mailer\SubscriberError;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class AmazonSESMapper {
|
||||
use BlacklistErrorMapperTrait;
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
const METHOD = Mailer::METHOD_AMAZONSES;
|
||||
|
||||
function getErrorFromException(\Exception $e, $subscriber) {
|
||||
$level = MailerError::LEVEL_HARD;
|
||||
if ($e instanceof \Swift_RfcComplianceException) {
|
||||
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\SubscriberError;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
trait BlacklistErrorMapperTrait {
|
||||
function getBlacklistError($subscriber) {
|
||||
$message = sprintf(WPFunctions::get()->__('%s has returned an unknown error.', 'mailpoet'), self::METHOD);
|
||||
$subscriber_errors = [new SubscriberError($subscriber, null)];
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_SOFT, $message, null, $subscriber_errors);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Mailer\SubscriberError;
|
||||
use MailPoet\Services\Bridge\API;
|
||||
use InvalidArgumentException;
|
||||
@ -14,8 +15,11 @@ if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
class MailPoetMapper {
|
||||
use BlacklistErrorMapperTrait;
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
const METHOD = Mailer::METHOD_MAILPOET;
|
||||
|
||||
const TEMPORARY_UNAVAILABLE_RETRY_INTERVAL = 300; // seconds
|
||||
|
||||
function getInvalidApiKeyError() {
|
||||
|
@ -7,8 +7,11 @@ use MailPoet\Mailer\SubscriberError;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class PHPMailMapper {
|
||||
use BlacklistErrorMapperTrait;
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
const METHOD = Mailer::METHOD_PHPMAIL;
|
||||
|
||||
function getErrorFromException(\Exception $e, $subscriber) {
|
||||
$level = MailerError::LEVEL_HARD;
|
||||
if (strpos($e->getMessage(), 'Invalid address') === 0) {
|
||||
|
@ -7,8 +7,11 @@ use MailPoet\Mailer\SubscriberError;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class SMTPMapper {
|
||||
use BlacklistErrorMapperTrait;
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
const METHOD = Mailer::METHOD_SMTP;
|
||||
|
||||
/**
|
||||
* @see https://swiftmailer.symfony.com/docs/sending.html
|
||||
* @return MailerError
|
||||
|
@ -7,8 +7,11 @@ use MailPoet\Mailer\SubscriberError;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class SendGridMapper {
|
||||
use BlacklistErrorMapperTrait;
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
const METHOD = Mailer::METHOD_SENDGRID;
|
||||
|
||||
function getErrorFromResponse($response, $subscriber) {
|
||||
$response = (!empty($response['errors'][0])) ?
|
||||
$response['errors'][0] :
|
||||
|
@ -12,6 +12,8 @@ use MailPoet\Services\Bridge\API;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class MailPoet {
|
||||
use BlacklistTrait;
|
||||
|
||||
public $api;
|
||||
public $sender;
|
||||
public $reply_to;
|
||||
@ -37,6 +39,14 @@ class MailPoet {
|
||||
return Mailer::formatMailerErrorResult($this->error_mapper->getInvalidApiKeyError());
|
||||
}
|
||||
|
||||
$subscribers_for_blacklist_check = is_array($subscriber) ? $subscriber : [$subscriber];
|
||||
foreach ($subscribers_for_blacklist_check as $sub) {
|
||||
if ($this->isBlacklisted($sub)) {
|
||||
$error = $this->error_mapper->getBlacklistError($sub);
|
||||
return Mailer::formatMailerErrorResult($error);
|
||||
}
|
||||
}
|
||||
|
||||
$message_body = $this->getBody($newsletter, $subscriber, $extra_params);
|
||||
$result = $this->api->sendMessages($message_body);
|
||||
|
||||
@ -125,4 +135,8 @@ class MailPoet {
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
|
||||
function checkBlacklist(array $subscribers) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ if (!defined('ABSPATH')) exit;
|
||||
require_once ABSPATH . WPINC . '/class-phpmailer.php';
|
||||
|
||||
class PHPMail {
|
||||
use BlacklistTrait;
|
||||
|
||||
public $sender;
|
||||
public $reply_to;
|
||||
public $return_path;
|
||||
@ -29,6 +31,10 @@ class PHPMail {
|
||||
}
|
||||
|
||||
function send($newsletter, $subscriber, $extra_params = []) {
|
||||
if ($this->isBlacklisted($subscriber)) {
|
||||
$error = $this->error_mapper->getBlacklistError($subscriber);
|
||||
return Mailer::formatMailerErrorResult($error);
|
||||
}
|
||||
try {
|
||||
$mailer = $this->configureMailerWithMessage($newsletter, $subscriber, $extra_params);
|
||||
$result = $mailer->send();
|
||||
|
@ -8,6 +8,8 @@ use MailPoet\WP\Functions as WPFunctions;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class SMTP {
|
||||
use BlacklistTrait;
|
||||
|
||||
public $host;
|
||||
public $port;
|
||||
public $authentication;
|
||||
@ -48,6 +50,10 @@ class SMTP {
|
||||
}
|
||||
|
||||
function send($newsletter, $subscriber, $extra_params = []) {
|
||||
if ($this->isBlacklisted($subscriber)) {
|
||||
$error = $this->error_mapper->getBlacklistError($subscriber);
|
||||
return Mailer::formatMailerErrorResult($error);
|
||||
}
|
||||
try {
|
||||
$message = $this->createMessage($newsletter, $subscriber, $extra_params);
|
||||
$result = $this->mailer->send($message);
|
||||
|
@ -9,6 +9,8 @@ use MailPoet\WP\Functions as WPFunctions;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class SendGrid {
|
||||
use BlacklistTrait;
|
||||
|
||||
public $url = 'https://api.sendgrid.com/api/mail.send.json';
|
||||
public $api_key;
|
||||
public $sender;
|
||||
@ -28,6 +30,10 @@ class SendGrid {
|
||||
}
|
||||
|
||||
function send($newsletter, $subscriber, $extra_params = []) {
|
||||
if ($this->isBlacklisted($subscriber)) {
|
||||
$error = $this->error_mapper->getBlacklistError($subscriber);
|
||||
return Mailer::formatMailerErrorResult($error);
|
||||
}
|
||||
$result = $this->wp->wpRemotePost(
|
||||
$this->url,
|
||||
$this->request($newsletter, $subscriber, $extra_params)
|
||||
|
24
lib/Subscription/Blacklist.php
Normal file
24
lib/Subscription/Blacklist.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace MailPoet\Subscription;
|
||||
|
||||
class Blacklist {
|
||||
const SALT = 'mailpoet';
|
||||
|
||||
private $blacklist = [
|
||||
'e60c6e0e73997c92d4ceac78a6b6cbbe6249244c4106a3c31de421fc50370ecd' => 1,
|
||||
];
|
||||
|
||||
public function isBlacklisted($email) {
|
||||
$hashed_email = $this->hash($email);
|
||||
return !empty($this->blacklist[$hashed_email]);
|
||||
}
|
||||
|
||||
public function hash($email) {
|
||||
return hash('sha256', $email . self::SALT);
|
||||
}
|
||||
|
||||
public function addEmail($email) {
|
||||
$hashed_email = $this->hash($email);
|
||||
$this->blacklist[$hashed_email] = 1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user