Skip to content
Merged
37 changes: 21 additions & 16 deletions code/email/MailblockMailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function onMessage(MessageEvent $event): void
$overrideConfiguration = $siteConfig->getField('MailblockOverrideConfiguration');
$sendAllTo = Email::getSendAllEmailsTo();

if ($enabled
if (
$enabled
&& ($enabledOnLive || !Director::isLive())
&& (!$sendAllTo || $overrideConfiguration)
) {
Expand All @@ -53,32 +54,37 @@ public function onMessage(MessageEvent $event): void
$bccRecipients = [];

$subject = $message->getSubject();
foreach ($message->getTo() as $to) {
$recipients[] = $to->getAddress();
}
foreach ($message->getCc() as $cc) {
$ccRecipients[] = $cc->getAddress();
}
foreach ($message->getBcc() as $bcc) {
$bccRecipients[] = $bcc->getAddress();
}
foreach ($message->getTo() as $to) {
$recipients[] = $to->getAddress();
}
foreach ($message->getCc() as $cc) {
$ccRecipients[] = $cc->getAddress();
}
foreach ($message->getBcc() as $bcc) {
$bccRecipients[] = $bcc->getAddress();
}

$recipients = implode(',', $recipients);
$ccRecipients = implode(',', $ccRecipients);
$bccRecipients = implode(',', $bccRecipients);
$recipients = implode(',', $recipients);
$ccRecipients = implode(',', $ccRecipients);
$bccRecipients = implode(',', $bccRecipients);

$mailblockRecipients = $siteConfig->getField('MailblockRecipients');

$subject .= " [addressed to $recipients";
if ($ccRecipients) $subject .= ", cc to $ccRecipients";
if ($bccRecipients) $subject .= ", bcc to $bccRecipients";
if ($ccRecipients) {
$subject .= ", cc to $ccRecipients";
}
if ($bccRecipients) {
$subject .= ", bcc to $bccRecipients";
}
$subject .= ']';
$message->setSubject($subject);

// If one of the orignial recipients is in the whitelist, add them
// to the new recipients list.
$mailblockWhitelist = $siteConfig->getField('MailblockWhitelist');
$whitelist = !empty($mailblockWhitelist) ? preg_split("/\r\n|\n|\r/", $mailblockWhitelist) : [];
$newRecipients = !empty($mailblockRecipients) ? preg_split("/\r\n|\n|\r/", $mailblockRecipients) : [];
$cc = [];
$bcc = [];
foreach ($whitelist as $whiteListed) {
Expand All @@ -94,7 +100,6 @@ public function onMessage(MessageEvent $event): void
}
}
}
$newRecipients = !empty($mailblockRecipients) ? preg_split("/\r\n|\n|\r/", $mailblockRecipients) : [];
$message->setTo($newRecipients);
$message->setBcc($bcc);
$message->setCc($cc);
Expand Down
Loading
Loading