17
17
use Symfony \Component \Mailer \Transport \TransportInterface ;
18
18
use Symfony \Component \Messenger \MessageBusInterface ;
19
19
use Symfony \Component \Mime \RawMessage ;
20
+ use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
20
21
21
22
/**
22
23
* @author Fabien Potencier <[email protected] >
@@ -25,8 +26,9 @@ class Mailer implements MailerInterface
25
26
{
26
27
private $ transport ;
27
28
private $ bus ;
29
+ private $ dispatcher ;
28
30
29
- public function __construct (TransportInterface $ transport , MessageBusInterface $ bus = null )
31
+ public function __construct (TransportInterface $ transport , MessageBusInterface $ bus = null , EventDispatcherInterface $ dispatcher = null )
30
32
{
31
33
$ this ->transport = $ transport ;
32
34
$ this ->bus = $ bus ;
@@ -40,18 +42,20 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void
40
42
return ;
41
43
}
42
44
43
- $ message = clone $ message ;
44
- if (null !== $ envelope ) {
45
- $ envelope = clone $ envelope ;
46
- } else {
47
- try {
48
- $ envelope = new DelayedSmtpEnvelope ($ message );
49
- } catch (\Exception $ e ) {
50
- throw new TransportException ('Cannot send message without a valid envelope. ' , 0 , $ e );
45
+ if (null !== $ this ->dispatcher ) {
46
+ $ message = clone $ message ;
47
+ if (null !== $ envelope ) {
48
+ $ envelope = clone $ envelope ;
49
+ } else {
50
+ try {
51
+ $ envelope = new DelayedSmtpEnvelope ($ message );
52
+ } catch (\Exception $ e ) {
53
+ throw new TransportException ('Cannot send message without a valid envelope. ' , 0 , $ e );
54
+ }
51
55
}
56
+ $ event = new MessageEvent ($ message , $ envelope , $ this ->transport ->getName ());
57
+ $ this ->dispatcher ->dispatch ($ event );
52
58
}
53
- $ event = new MessageEvent ($ message , $ envelope , $ this ->transport ->getName ());
54
- $ this ->dispatcher ->dispatch ($ event );
55
59
56
60
$ this ->bus ->dispatch (new SendEmailMessage ($ message , $ envelope ));
57
61
}
0 commit comments