Skip to content

Commit

Permalink
[FEATURE] Introduction of flag to determine if Message has been inter…
Browse files Browse the repository at this point in the history
…cepted by the DebuggingAspect to prevent double interception

(if FormatD.Mailer.QueueAdaptor is used the message is ->send() two times)
  • Loading branch information
bweinzierl committed May 18, 2021
1 parent 9e04b71 commit 4871a25
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Classes/Aspect/DebuggingAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* @Flow\Aspect
* @Flow\Introduce("class(Neos\SwiftMailer\Message)", traitName="FormatD\Mailer\Traits\InterceptionTrait")
*/
class DebuggingAspect {

Expand All @@ -18,7 +19,7 @@ class DebuggingAspect {
* @var array
*/
protected $settings;

/**
* Intercept all emails or add bcc according to package configuration
*
Expand All @@ -35,6 +36,7 @@ public function interceptEmails(\Neos\Flow\Aop\JoinPointInterface $joinPoint) {
$message = $joinPoint->getProxy();

if ($this->settings['interceptAll']['active']) {

$oldTo = $message->getTo();
$oldCc = $message->getCc();
$oldBcc = $message->getBcc();
Expand All @@ -48,8 +50,14 @@ public function interceptEmails(\Neos\Flow\Aop\JoinPointInterface $joinPoint) {
}
}

// stop if this aspect is executed twice (happens if QueueAdaptor is installed)
if ($message->isIntercepted()) {
return;
}

$interceptedRecipients = key($oldTo) . ($oldCc ? ' CC: ' . key($oldCc) : '') . ($oldBcc ? ' BCC: ' . key($oldBcc) : '');
$message->setSubject('[intercepted '.$interceptedRecipients.'] '.$message->getSubject());
$message->setIntercepted(true);

$message->setCc(array());
$message->setBcc(array());
Expand Down
36 changes: 36 additions & 0 deletions Classes/Traits/InterceptionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace FormatD\Mailer\Traits;

/* *
* This script belongs to the Flow package "FormatD.Mailer". *
* */

use Neos\Flow\Annotations as Flow;


trait InterceptionTrait {

/**
* @var boolean
*/
protected $intercepted = false;

/**
* @return bool
*/
public function isIntercepted(): bool
{
return $this->intercepted;
}

/**
* @param bool $intercepted
*/
public function setIntercepted(bool $intercepted): void
{
$this->intercepted = $intercepted;
}

}

?>

0 comments on commit 4871a25

Please sign in to comment.