Skip to content

Commit f82d9b1

Browse files
authored
Merge pull request #5 from Format-D/update-for-symfonymailer
Update for SymfonyMailer (but still targeting Neos 8)
2 parents d7f1f24 + 4d558ea commit f82d9b1

8 files changed

Lines changed: 417 additions & 266 deletions

File tree

Classes/Aspect/DebuggingAspect.php

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,57 @@
11
<?php
2+
23
namespace FormatD\Mailer\Aspect;
34

4-
/* *
5-
* This script belongs to the Flow package "FormatD.Mailer". *
6-
* */
5+
/*
6+
* This file is part of the FormatD.Mailer package.
7+
*/
78

9+
use FormatD\Mailer\Transport\FdMailerTransport;
10+
use FormatD\Mailer\Transport\InterceptingTransport;
811
use Neos\Flow\Annotations as Flow;
9-
12+
use Neos\Flow\Aop\JoinPointInterface;
13+
use Symfony\Component\Mailer\Mailer;
14+
use Symfony\Component\Mailer\MailerInterface;
15+
use Symfony\Component\Mailer\Transport;
1016

1117
/**
1218
* @Flow\Aspect
13-
* @Flow\Introduce("class(Neos\SwiftMailer\Message)", traitName="FormatD\Mailer\Traits\InterceptionTrait")
1419
*/
15-
class DebuggingAspect {
16-
17-
/**
18-
* @Flow\InjectConfiguration(type="Settings", package="FormatD.Mailer")
19-
* @var array
20-
*/
21-
protected $settings;
22-
23-
/**
24-
* Intercept all emails or add bcc according to package configuration
25-
*
26-
* @param \Neos\Flow\Aop\JoinPointInterface $joinPoint
27-
* @Flow\Before("method(Neos\SwiftMailer\Message->send())")
28-
* @return void
29-
*/
30-
public function interceptEmails(\Neos\Flow\Aop\JoinPointInterface $joinPoint) {
31-
32-
if ($this->settings['interceptAll']['active'] || $this->settings['bccAll']['active']) {
33-
/**
34-
* @var \Neos\SwiftMailer\Message $message
35-
*/
36-
$message = $joinPoint->getProxy();
37-
38-
if ($this->settings['interceptAll']['active']) {
39-
40-
$oldTo = $message->getTo();
41-
$oldCc = $message->getCc();
42-
$oldBcc = $message->getBcc();
43-
44-
foreach ($this->settings['interceptAll']['noInterceptPatterns'] as $pattern) {
45-
if (preg_match($pattern, key($oldTo))) {
46-
// let the mail through but clean all cc and bcc fields
47-
$message->setCc(array());
48-
$message->setBcc(array());
49-
return;
50-
}
51-
}
52-
53-
// stop if this aspect is executed twice (happens if QueueAdaptor is installed)
54-
if ($message->isIntercepted()) {
55-
return;
56-
}
57-
58-
$interceptedRecipients = key($oldTo) . ($oldCc ? ' CC: ' . key($oldCc) : '') . ($oldBcc ? ' BCC: ' . key($oldBcc) : '');
59-
$message->setSubject('[intercepted '.$interceptedRecipients.'] '.$message->getSubject());
60-
$message->setIntercepted(true);
61-
62-
$message->setCc(array());
63-
$message->setBcc(array());
64-
65-
$first = true;
66-
foreach ($this->settings['interceptAll']['recipients'] as $email) {
67-
if ($first) {
68-
$message->setTo($email);
69-
} else {
70-
$message->addCc($email);
71-
}
72-
$first = false;
73-
}
74-
}
75-
76-
if ($this->settings['bccAll']['active']) {
77-
foreach ($this->settings['bccAll']['recipients'] as $email) {
78-
$message->addBcc($email);
79-
}
80-
}
20+
class DebuggingAspect
21+
{
22+
#[Flow\InjectConfiguration(package: 'FormatD.Mailer', type: 'Settings')]
23+
protected array $settings;
24+
25+
#[Flow\InjectConfiguration(path: 'mailer', package: 'Neos.SymfonyMailer')]
26+
protected array $symfonyMailerSettings;
27+
28+
protected ?Mailer $mailer = null;
29+
30+
/**
31+
* @Flow\Around("method(Neos\SymfonyMailer\Service\MailerService->getMailer())")
32+
*/
33+
public function decorateMailer(JoinPointInterface $joinPoint): MailerInterface
34+
{
35+
if (!($this->settings['interceptAll']['active'] ?? false) && !($this->settings['bccAll']['active'] ?? false)) {
36+
if ($this->mailer === null && ($this->symfonyMailerSettings['dsn'] ?? null) === 'fd-mailer') {
37+
$this->mailer = new Mailer(new FdMailerTransport());
38+
} else {
39+
$this->mailer = $joinPoint->getAdviceChain()->proceed($joinPoint);
40+
}
41+
return $this->mailer;
8142
}
82-
}
83-
}
8443

85-
?>
44+
if ($this->mailer === null) {
45+
$dsn = $this->symfonyMailerSettings['dsn'];
46+
if ($dsn === 'fd-mailer') {
47+
$actualTransport = new FdMailerTransport();
48+
} else {
49+
$actualTransport = Transport::fromDsn($this->symfonyMailerSettings['dsn']);
50+
}
51+
$interceptingTransport = new InterceptingTransport($actualTransport);
52+
$this->mailer = new Mailer($interceptingTransport);
53+
}
54+
55+
return $this->mailer;
56+
}
57+
}

0 commit comments

Comments
 (0)