-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config for blacklist split to services (#5)
* add blacklist functionality * use interface instead of model remove phpdoc invert condition * fix codestyle * splitting validation by blacklist and log email to separate services * remove unnecessary try catch from regexp by pattern move call $proceed to service from plugin
- Loading branch information
Showing
6 changed files
with
177 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* @author Mygento Team | ||
* @copyright 2023 Mygento (https://www.mygento.com) | ||
* @package Mygento_Smtp | ||
*/ | ||
|
||
namespace Mygento\Smtp\Model\Mail; | ||
|
||
use Closure; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\MailException; | ||
use Magento\Framework\Mail\Address; | ||
use Magento\Framework\Mail\EmailMessageInterface; | ||
use Mygento\Smtp\Api\Data; | ||
use Mygento\Smtp\Api\LogRepositoryInterface; | ||
use Mygento\Smtp\Model\Source\Status; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Processor | ||
{ | ||
public function __construct( | ||
private LogRepositoryInterface $repo, | ||
private Data\LogInterfaceFactory $factory, | ||
private LoggerInterface $logger | ||
) { | ||
} | ||
|
||
public function process(Closure $proceed, EmailMessageInterface $message): void | ||
{ | ||
/** @var Data\LogInterface $entity */ | ||
$entity = $this->factory->create(); | ||
|
||
try { | ||
$this->fillEntity($entity, $message); | ||
$proceed(); | ||
$entity->setStatus(Status::STATUS_SUCCESS); | ||
} catch (MailException $e) { | ||
$entity->setError($e->getPrevious()->getMessage()); | ||
$entity->setStatus(Status::STATUS_ERROR); | ||
|
||
throw $e; | ||
} catch (LocalizedException $e) { | ||
$entity->setStatus(Status::STATUS_ERROR); | ||
$entity->setError($e->getMessage()); | ||
$this->logger->error($e->getMessage(), ['exception' => $e]); | ||
} finally { | ||
$this->repo->save($entity); | ||
} | ||
} | ||
|
||
private function formatList(iterable $list): string | ||
{ | ||
return implode( | ||
',', | ||
array_map(fn (Address $address) => $address->getEmail(), $list) | ||
); | ||
} | ||
|
||
private function fillEntity(Data\LogInterface $entity, EmailMessageInterface $message): Data\LogInterface | ||
{ | ||
if ($message->getSender()) { | ||
$entity->setSender( | ||
$message->getSender()->getName() . ' <' . $message->getSender()->getEmail() . '>' | ||
); | ||
} elseif (is_iterable($message->getFrom()) && count($message->getFrom()) > 0) { | ||
$f = $message->getFrom(); | ||
$from = reset($f); | ||
$entity->setSender( | ||
$from->getName() . ' <' . $from->getEmail() . '>' | ||
); | ||
} | ||
|
||
$entity->setRecipient($this->formatList($message->getTo())); | ||
$entity->setSubject($message->getSubject()); | ||
$messageBody = quoted_printable_decode($message->getBodyText()); | ||
$content = htmlspecialchars($messageBody); | ||
$entity->setContent($content); | ||
|
||
if (is_iterable($message->getCc()) && count($message->getCc()) > 0) { | ||
$entity->setCc($this->formatList($message->getCc())); | ||
} | ||
if (is_iterable($message->getBcc()) && count($message->getBcc()) > 0) { | ||
$entity->setBcc($this->formatList($message->getBcc())); | ||
} | ||
|
||
return $entity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* @author Mygento Team | ||
* @copyright 2023 Mygento (https://www.mygento.com) | ||
* @package Mygento_Smtp | ||
*/ | ||
|
||
namespace Mygento\Smtp\Model\Mail; | ||
|
||
use Magento\Framework\Mail\EmailMessageInterface; | ||
use Mygento\Smtp\Model\Config; | ||
|
||
class Validator | ||
{ | ||
public function __construct( | ||
private Config $config, | ||
) { | ||
} | ||
|
||
public function isValid(EmailMessageInterface $message): bool | ||
{ | ||
return !$this->inBlacklist($message); | ||
} | ||
|
||
private function inBlacklist(EmailMessageInterface $message): bool | ||
{ | ||
$blacklist = $this->config->getBlacklist(); | ||
|
||
if (!$blacklist) { | ||
return false; | ||
} | ||
|
||
$recipient = $this->getRecipient($message); | ||
$patterns = array_unique(explode(PHP_EOL, $blacklist)); | ||
foreach ($patterns as $pattern) { | ||
if (preg_match($pattern, $recipient)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function getRecipient(EmailMessageInterface $message): string | ||
{ | ||
$emails = []; | ||
if ($message->getTo()) { | ||
foreach ($message->getTo() as $address) { | ||
$emails[] = $address->getEmail(); | ||
} | ||
} | ||
|
||
return implode(',', $emails); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,14 @@ | |
<field id="log">1</field> | ||
</depends> | ||
</field> | ||
<field id="blacklist" translate="label comment" type="textarea" sortOrder="170" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Blacklist</label> | ||
<comment><![CDATA[Enter the main email pattern format. Separated by line breaks.<br /> | ||
For the email having the same pattern format with this main pattern, it can not receive email from the system.<br /> | ||
For example: Enter the pattern: /^[0-9][a-z0-9\$\%\&]+@[a-z]+\.[a-z]{2,}$/ means that email format must start with number, the following characters can be normal text, number or special character ($, %, &). The domain after @ with normal-text characters will not receive email from system. For instance: [email protected]</br> | ||
]]> | ||
</comment> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
|