Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions Plugin/Magento/Framework/Mail/TransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function __construct(
* @param \Magento\Framework\Mail\TransportInterface $subject
* @param Closure $proceed
* @return Closure|void $proceed
* @throws ReflectionException
*/
public function aroundSendMessage(
\Magento\Framework\Mail\TransportInterface $subject,
Expand All @@ -46,8 +45,8 @@ public function aroundSendMessage(
return $proceed();
}

if ($this->emailcatcher->blackListEnabled() && in_array($this->getToEmailAddress($subject), $this->getBlacklistEmailAddresses())) {
$subject->getMessage()->setSubject('Prevent Being Sent - Blacklisted Email Address');
if ($this->emailcatcher->blackListEnabled() && $this->emailInBlacklist($this->getToEmailAddress($subject))) {
$subject->getMessage()?->setSubject('Prevent Being Sent - Blacklisted Email Address');
$this->saveMessage($subject);

return;
Expand Down Expand Up @@ -88,7 +87,7 @@ private function getTemplateWhitelist(): array
/**
* @return array
*/
protected function getBlacklistEmailAddresses() : array
private function getBlacklistEmailAddresses() : array
{
return explode(',', $this->scopeConfig->getValue('emailcatcher/blacklist/block_email_addresses'));
}
Expand All @@ -97,8 +96,33 @@ protected function getBlacklistEmailAddresses() : array
* @param $subject
* @return string
*/
protected function getToEmailAddress($subject): string
private function getToEmailAddress($subject): string
{
return $subject->getMessage()->getTo()[0]->getEmail() ?? '';
}

/**
* @param string $emailAddress
* @return bool
*/
private function domainInBlacklist(string $emailAddress): bool
{
$blacklistedDomains = explode(',', $this->scopeConfig->getValue('emailcatcher/blacklist/block_email_domains'));
$emailDomain = substr(strrchr($emailAddress, "@"), 1);

return in_array($emailDomain, $blacklistedDomains);
}

/**
* @param string $emailAddress
* @return bool
*/
public function emailInBlacklist(string $emailAddress): bool
{
if ($this->domainInBlacklist($emailAddress)) {
return true;
}

return in_array($emailAddress, $this->getBlacklistEmailAddresses());
}
}
7 changes: 3 additions & 4 deletions Plugin/Magento/Framework/Mail/TransportInterfacePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ public function aroundSendMessage(
{
if (!$this->scopeConfig->isSetFlag('system/smtp/disable', ScopeInterface::SCOPE_STORE)) {
$proceed();
return;
}

if ($this->emailcatcher->developmentAdminAllowedEnabled()) {
$emailAddresses = [];
foreach ($subject->getMessage()->getTo() ?? [] as $to) {
foreach ($subject->getMessage()?->getTo() ?? [] as $to) {
$emailAddresses[] = $to->getEmail();
}
foreach ($subject->getMessage()->getCc() ?? [] as $cc) {
foreach ($subject->getMessage()?->getCc() ?? [] as $cc) {
$emailAddresses[] = $cc->getEmail();
}
foreach ($subject->getMessage()->getBcc() ?? [] as $bcc) {
foreach ($subject->getMessage()?->getBcc() ?? [] as $bcc) {
$emailAddresses[] = $bcc->getEmail();
}
if ($this->containsOnlyAdminUsers($emailAddresses)) {
Expand Down
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<field id="apply_blacklist">1</field>
</depends>
</field>
<field id="block_email_domains" type="textarea" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Block Email Domains</label>
<depends>
<field id="apply_blacklist">1</field>
</depends>
</field>
</group>
<group id="development" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>Development</label>
Expand Down