4
4
5
5
namespace MauticPlugin \SparkpostBundle \EventSubscriber ;
6
6
7
+ use Mautic \CoreBundle \Helper \CoreParametersHelper ;
7
8
use Mautic \EmailBundle \EmailEvents ;
8
9
use Mautic \EmailBundle \Event \TransportWebhookEvent ;
9
10
use Mautic \EmailBundle \Model \TransportCallback ;
10
11
use Mautic \LeadBundle \Entity \DoNotContact ;
12
+ use MauticPlugin \SparkpostBundle \Mailer \Transport \SparkpostTransport ;
11
13
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
14
+ use Symfony \Component \HttpFoundation \Response ;
15
+ use Symfony \Component \Mailer \Transport \Dsn ;
12
16
13
17
class CallbackSubscriber implements EventSubscriberInterface
14
18
{
15
- public function __construct (private TransportCallback $ transportCallback )
16
- {
19
+ public function __construct (
20
+ private TransportCallback $ transportCallback ,
21
+ private CoreParametersHelper $ coreParametersHelper
22
+ ) {
17
23
}
18
24
19
25
/**
@@ -28,41 +34,49 @@ public static function getSubscribedEvents(): array
28
34
29
35
public function processCallbackRequest (TransportWebhookEvent $ event ): void
30
36
{
37
+ $ dsn = Dsn::fromString ($ this ->coreParametersHelper ->get ('mailer_dsn ' ));
38
+
39
+ if (SparkpostTransport::MAUTIC_SPARKPOST_API_SCHEME !== $ dsn ->getScheme ()) {
40
+ return ;
41
+ }
42
+
31
43
$ payload = $ event ->getRequest ()->request ->all ();
32
44
33
45
foreach ($ payload as $ msys ) {
34
- $ msys = $ msys ['msys ' ] ?? null ;
35
- $ event = $ msys ['message_event ' ] ?? $ msys ['unsubscribe_event ' ] ?? null ;
46
+ $ msys = $ msys ['msys ' ] ?? null ;
47
+ $ messageEvent = $ msys ['message_event ' ] ?? $ msys ['unsubscribe_event ' ] ?? null ;
36
48
37
- if (!$ event ) {
49
+ if (!$ messageEvent ) {
38
50
continue ;
39
51
}
40
52
41
- if (isset ($ event ['rcpt_type ' ]) && 'to ' !== $ event ['rcpt_type ' ]) {
53
+ if (isset ($ messageEvent ['rcpt_type ' ]) && 'to ' !== $ messageEvent ['rcpt_type ' ]) {
42
54
// Ignore cc/bcc
43
55
continue ;
44
56
}
45
57
46
- $ type = $ event ['type ' ] ?? null ;
47
- $ bounceClass = $ event ['bounce_class ' ] ?? null ;
58
+ $ type = $ messageEvent ['type ' ] ?? null ;
59
+ $ bounceClass = $ messageEvent ['bounce_class ' ] ?? null ;
48
60
49
61
if ('bounce ' === $ type && !in_array ((int ) $ bounceClass , [10 , 30 , 50 , 51 , 52 , 53 , 54 , 90 ])) {
50
62
// Only parse hard bounces
51
63
// https://support.sparkpost.com/customer/portal/articles/1929896-bounce-classification-codes
52
64
continue ;
53
65
}
54
66
55
- $ hashId = $ event ['rcpt_meta ' ]['hashId ' ] ?? null ;
67
+ $ hashId = $ messageEvent ['rcpt_meta ' ]['hashId ' ] ?? null ;
56
68
57
69
if ($ hashId ) {
58
- $ this ->processCallbackByHashId ($ hashId , $ event );
70
+ $ this ->processCallbackByHashId ($ hashId , $ messageEvent );
59
71
60
72
continue ;
61
73
}
62
74
63
- $ rcptTo = $ event ['rcpt_to ' ] ?? '' ;
64
- $ this ->processCallbackByEmailAddress ($ rcptTo , $ event );
75
+ $ rcptTo = $ messageEvent ['rcpt_to ' ] ?? '' ;
76
+ $ this ->processCallbackByEmailAddress ($ rcptTo , $ messageEvent );
65
77
}
78
+
79
+ $ event ->setResponse (new Response ('Callback processed ' ));
66
80
}
67
81
68
82
/**
0 commit comments