This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCallbackSubscriber.php
More file actions
137 lines (115 loc) · 4.51 KB
/
CallbackSubscriber.php
File metadata and controls
137 lines (115 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace MauticPlugin\SparkpostBundle\EventSubscriber;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event\TransportWebhookEvent;
use Mautic\EmailBundle\Model\TransportCallback;
use Mautic\LeadBundle\Entity\DoNotContact;
use MauticPlugin\SparkpostBundle\Mailer\Transport\SparkpostTransport;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Transport\Dsn;
class CallbackSubscriber implements EventSubscriberInterface
{
public function __construct(
private TransportCallback $transportCallback,
private CoreParametersHelper $coreParametersHelper
) {
}
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
EmailEvents::ON_TRANSPORT_WEBHOOK => 'processCallbackRequest',
];
}
public function processCallbackRequest(TransportWebhookEvent $event): void
{
$dsn = Dsn::fromString($this->coreParametersHelper->get('mailer_dsn'));
if (SparkpostTransport::MAUTIC_SPARKPOST_API_SCHEME !== $dsn->getScheme()) {
return;
}
$payload = $event->getRequest()->request->all();
foreach ($payload as $msys) {
$msys = $msys['msys'] ?? null;
$messageEvent = $msys['message_event'] ?? $msys['unsubscribe_event'] ?? null;
if (!$messageEvent) {
continue;
}
if (isset($messageEvent['rcpt_type']) && 'to' !== $messageEvent['rcpt_type']) {
// Ignore cc/bcc
continue;
}
$type = $messageEvent['type'] ?? null;
$bounceClass = $messageEvent['bounce_class'] ?? null;
if ('bounce' === $type && !in_array((int) $bounceClass, [10, 30, 50, 51, 52, 53, 54, 90]) || ('out_of_band' === $type && (int) $bounceClass === 60)) {
// Only parse hard bounces
// https://support.sparkpost.com/customer/portal/articles/1929896-bounce-classification-codes
continue;
}
$hashId = $messageEvent['rcpt_meta']['hashId'] ?? null;
if ($hashId) {
$this->processCallbackByHashId($hashId, $messageEvent);
continue;
}
$rcptTo = $messageEvent['rcpt_to'] ?? '';
$this->processCallbackByEmailAddress($rcptTo, $messageEvent);
}
$event->setResponse(new Response('Callback processed'));
}
/**
* @param string $hashId
* @param array<mixed> $event
*/
private function processCallbackByHashId($hashId, array $event): void
{
$type = $event['type'] ?? null;
switch ($type) {
case 'policy_rejection':
case 'out_of_band':
case 'bounce':
$rawReason = $event['raw_reason'] ?? '';
$this->transportCallback->addFailureByHashId($hashId, $rawReason);
break;
case 'spam_complaint':
$fbType = $event['fbtype'] ?? '';
$this->transportCallback->addFailureByHashId($hashId, $fbType, DoNotContact::UNSUBSCRIBED);
break;
case 'list_unsubscribe':
case 'link_unsubscribe':
$this->transportCallback->addFailureByHashId($hashId, 'unsubscribed', DoNotContact::UNSUBSCRIBED);
break;
default:
break;
}
}
/**
* @param string $email
* @param array<mixed> $event
*/
private function processCallbackByEmailAddress($email, array $event): void
{
$type = $event['type'] ?? null;
switch ($type) {
case 'policy_rejection':
case 'out_of_band':
case 'bounce':
$rawReason = $event['raw_reason'] ?? '';
$this->transportCallback->addFailureByAddress($email, $rawReason);
break;
case 'spam_complaint':
$fbType = $event['fbtype'] ?? '';
$this->transportCallback->addFailureByAddress($email, $fbType, DoNotContact::UNSUBSCRIBED);
break;
case 'list_unsubscribe':
case 'link_unsubscribe':
$this->transportCallback->addFailureByAddress($email, 'unsubscribed', DoNotContact::UNSUBSCRIBED);
break;
default:
break;
}
}
}