8000 [Webhook] Add Mailchimp webhook (fixes #50285) · symfony/symfony@b88024d · GitHub
[go: up one dir, main page]

Skip to content

Commit b88024d

Browse files
committed
[Webhook] Add Mailchimp webhook (fixes #50285)
1 parent 0ce31a5 commit b88024d

File tree

25 files changed

+674
-0
lines changed

25 files changed

+674
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
26662666
$webhookRequestParsers = [
26672667
MailerBridge\Brevo\Webhook\BrevoRequestParser::class => 'mailer.webhook.request_parser.brevo',
26682668
MailerBridge\MailerSend\Webhook\MailerSendRequestParser::class => 'mailer.webhook.request_parser.mailersend',
2669+
MailerBridge\Mailchimp\Webhook\MailchimpRequestParser::class => 'mailer.webhook.request_parser.mailchimp',
26692670
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
26702671
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
26712672
MailerBridge\Mailomat\Webhook\MailomatRequestParser::class => 'mailer.webhook.request_parser.mailomat',

src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer_webhook.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Mailer\Bridge\Brevo\RemoteEvent\BrevoPayloadConverter;
1515
use Symfony\Component\Mailer\Bridge\Brevo\Webhook\BrevoRequestParser;
16+
use Symfony\Component\Mailer\Bridge\Mailchimp\RemoteEvent\MailchimpPayloadConverter;
17+
use Symfony\Component\Mailer\Bridge\Mailchimp\Webhook\MailchimpRequestParser;
1618
use Symfony\Component\Mailer\Bridge\MailerSend\RemoteEvent\MailerSendPayloadConverter;
1719
use Symfony\Component\Mailer\Bridge\MailerSend\Webhook\MailerSendRequestParser;
1820
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\MailgunPayloadConverter;
@@ -76,5 +78,10 @@
7678
->set('mailer.webhook.request_parser.sweego', SweegoRequestParser::class)
7779
->args([service('mailer.payload_converter.sweego')])
7880
->alias(SweegoRequestParser::class, 'mailer.webhook.request_parser.sweego')
81+
82+
->set('mailer.payload_converter.mailchimp', MailchimpPayloadConverter::class)
83+
->set('mailer.webhook.request_parser.mailchimp', MailchimpRequestParser::class)
84+
->args([service('mailer.payload_converter.mailchimp')])
85+
->alias(MailchimpRequestParser::class, 'mailer.webhook.request_parser.mailchimp')
7986
;
8087
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Bridge\Mailchimp\RemoteEvent;
13+
14+
use Symfony\Component\RemoteEvent\Event\Mailer\AbstractMailerEvent;
15+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
16+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
17+
use Symfony\Component\RemoteEvent\Exception\ParseException;
18+
use Symfony\Component\RemoteEvent\PayloadConverterInterface;
19+
20+
final class MailchimpPayloadConverter implements PayloadConverterInterface
21+
{
22+
public function convert(array $payload): AbstractMailerEvent
23+
{
24+
if (\in_array($payload['event'], ['send', 'deferral', 'soft_bounce', 'hard_bounce', 'delivered', 'reject'], true)) {
25+
$name = match ($payload['event']) {
26+
'send' => MailerDeliveryEvent::RECEIVED,
27+
'deferral', => MailerDeliveryEvent::DEFERRED,
28+
'soft_bounce', 'hard_bounce' => MailerDeliveryEvent::BOUNCE,
29+
'delivered' => MailerDeliveryEvent::DELIVERED,
30+
'reje EF5E ct' => MailerDeliveryEvent::DROPPED,
31+
};
32+
33+
$event = new MailerDeliveryEvent($name, $payload['msg']['_id'], $payload);
34+
// reason is only available on failed messages
35+
$event->setReason($this->getReason($payload));
36+
} else {
37+
$name = match ($payload['event']) {
38+
'click' => MailerEngagementEvent::CLICK,
39+
'open' => MailerEngagementEvent::OPEN,
40+
'spam' => MailerEngagementEvent::SPAM,
41+
'unsub' => MailerEngagementEvent::UNSUBSCRIBE,
42+
default => throw new ParseException(\sprintf('Unsupported event "%s".', $payload['event'])),
43+
};
44+
$event = new MailerEngagementEvent($name, $payload['msg']['_id'], $payload);
45+
}
46+
47+
if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['msg']['ts'])) {
48+
throw new ParseException(sprintf('Invalid date "%s".', $payload['msg']['ts']));
49+
}
50+
$event->setDate($date);
51+
$event->setRecipientEmail($payload['msg']['email']);
52+
$event->setMetadata($payload['msg']['metadata']);
53+
$event->setTags($payload['msg']['tags']);
54+
55+
return $event;
56+
}
57+
58+
private function getReason(array $payload): string
59+
{
60+
if (null !== $payload['msg']['diag']) {
61+
return $payload['msg']['diag'];
62+
}
63+
if (null !== $payload['msg']['bounce_description']) {
64+
return $payload['msg']['bounce_description'];
65+
}
66+
if (!empty($msg['smtp_events'])) {
67+
$reasons = [];
68+
foreach ($msg['smtp_events'] as $event) {
69+
$reasons[] = \sprintf('type: %s diag: %s', $event['type'], $event['diag']);
70+
}
71+
// Return concatenated reasons or an empty string if no reasons found
72+
return implode(' ', $reasons);
73+
}
74+
75+
return '';
76+
}
77+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"mandrill_events": {
3+
"ts": 1365109999,
4+
"event": "click",
5+
"msg": {
6+
"ts": 1365109999,
7+
"subject": "Foo bar",
8+
"email": "foo@example.com",
9+
"sender": "bar@example.com",
10+
"tags": [
11+
"my_tag_1",
12+
"my_tag_2"
13+
],
14+
"smtp_events": null,
15+
"opens": [
16+
{
17+
"ts": 1365110000
18+
}
19+
],
20+
"clicks": [
21+
{
22+
"ts": 1365110000,
23+
"url": "https://www.example.com"
24+
}
25+
],
26+
"state": "sent",
27+
"metadata": {
28+
"mandrill-var-1": "foo",
29+
"mandrill-var-2": "bar"
30+
},
31+
"_id": "7761630",
32+
"_version": "123",
33+
"subaccount": null,
34+
"diag": null,
35+
"bounce_description": null,
36+
"template": null
37+
}
38+
},
39+
"X-Mandrill-Signature": "+Hk40WMuK9Ef/ebv0xBW7KOYdgw="
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '7761630', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events']);
6+
$wh->setRecipientEmail('foo@example.com');
7+
$wh->setTags(['my_tag_1', 'my_tag_2']);
8+
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10+
11+
return $wh;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"mandrill_events": {
3+
"ts": 1365109999,
4+
"event": "deferral",
5+
"msg": {
6+
"ts": 1365109999,
7+
"subject": "Foo bar",
8+
"email": "foo@example.com",
9+
"sender": "bar@example.com",
10+
"tags": [
11+
"my_tag_1",
12+
"my_tag_2"
13+
],
14+
"smtp_events": null,
15+
"opens": null,
16+
"clicks": null,
17+
"state": "sent",
18+
"metadata": {
19+
"mandrill-var-1": "foo",
20+
"mandrill-var-2": "bar"
21+
},
22+
"_id": "7761631",
23+
"_version": "123",
24+
"subaccount": null,
25+
"diag": null,
26+
"bounce_description": null,
27+
"template": null
28+
}
29+
},
30+
"X-Mandrill-Signature": "YxB6lg2eNzLGhUMjnB6+2Kb6AIk="
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
5+
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DEFERRED, '7761631', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events']);
6+
$wh->setRecipientEmail('foo@example.com');
7+
$wh->setTags(['my_tag_1', 'my_tag_2']);
8+
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10+
$wh->setReason('');
11+
12+
return $wh;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"mandrill_events": {
3+
"ts": 1365109999,
4+
"event": "delivered",
5+
"msg": {
6+
"ts": 1365109999,
7+
"subject": "Foo bar",
8+
"email": "foo@example.com",
9+
"sender": "bar@example.com",
10+
"tags": [
11+
"my_tag_1",
12+
"my_tag_2"
13+
],
14+
"smtp_events": null,
15+
"opens": null,
16+
"clicks": null,
17+
"state": "sent",
18+
"metadata": {
19+
"mandrill-var-1": "foo",
20+
"mandrill-var-2": "bar"
21+
},
22+
"_id": "7761632",
23+
"_version": "123",
24+
"subaccount": null,
25+
"diag": null,
26+
"bounce_description": null,
27+
"template": null
28+
}
29+
},
30+
"X-Mandrill-Signature": "7YD/bSKyk1H8aXyThI+TRbz+nRs="
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
5+
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DELIVERED, '7761632', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events']);
6+
$wh->setRecipientEmail('foo@example.com');
7+
$wh->setTags(['my_tag_1', 'my_tag_2']);
8+
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10+
return $wh;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"mandrill_events": {
3+
"ts": 1365109999,
4+
"event": "hard_bounce",
5+
"msg": {
6+
"ts": 1365109999,
7+
"subject": "Foo bar",
8+
"email": "foo@example.com",
9+
"sender": "bar@example.com",
10+
"tags": [
11+
"my_tag_1",
12+
"my_tag_2"
13+
],
14+
"smtp_events": null,
15+
"opens": null,
16+
"clicks": null,
17+
"state": "sent",
18+
"metadata": {
19+
"mandrill-var-1": "foo",
20+
"mandrill-var-2": "bar"
21+
},
22+
"_id": "7761633",
23+
"_version": "123",
24+
"subaccount": null,
25+
"diag": null,
26+
"bounce_description": "hard bounce",
27+
"template": null
28+
}
29+
},
30+
"X-Mandrill-Signature": "Dcz4HUG731pt489yED5eAzgEMrc="
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
5+
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::BOUNCE, '7761633', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events']);
6+
$wh->setRecipientEmail('foo@example.com');
7+
$wh->setTags(['my_tag_1', 'my_tag_2']);
8+
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10+
$wh->setReason('hard bounce');
11+
return $wh;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"mandrill_events": {
3+
"ts": 1365109999,
4+
"event": "open",
5+
"msg": {
6+
"ts": 1365109999,
7+
"subject": "Foo bar",
8+
"email": "foo@example.com",
9+
"sender": "bar@example.com",
10+
"tags": [
11+
"my_tag_1",
12+
"my_tag_2"
13+
],
14+
"smtp_events": null,
15+
"opens": [
16+
{
17+
"ts": 1365110000
18+
}
19+
],
20+
"clicks": null,
21+
"state": "sent",
22+
"metadata": {
23+
"mandrill-var-1": "foo",
24+
"mandrill-var-2": "bar"
25+
},
26+
"_id": "7761634",
27+
"_version": "123",
28+
"subaccount": null,
29+
"diag": null,
30+
"bounce_description": null,
31+
"template": null
32+
}
33+
},
34+
"X-Mandrill-Signature": "pWPElKIMolTDzVrQcev5sfqmFLo="
35+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::OPEN, '7761634', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events']);
6+
$wh->setRecipientEmail('foo@example.com');
7+
$wh->setTags(['my_tag_1', 'my_tag_2']);
8+
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10+
11+
return $wh;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"mandrill_events": {
3+
"ts": 1365109999,
4+
"event": "reject",
5+
"msg": {
6+
"ts": 1365109999,
7+
"subject": "Foo bar",
8+
"email": "foo@example.com",
9+
"sender": "bar@example.com",
10+
"tags": [
11+
"my_tag_1",
12+
"my_tag_2"
13+
],
14+
"smtp_events": null,
15+
"opens": null,
16+
"clicks": null,
17+
"state": "sent",
18+
"metadata": {
19+
"mandrill-var-1": "foo",
20+
"mandrill-var-2": "bar"
21+
},
22+
"_id": "7761635",
23+
"_version": "123",
24+
"subaccount": null,
25+
"diag": null,
26+
"bounce_description": null,
27+
"template": null
28+
}
29+
},
30+
"X-Mandrill-Signature": "uyKNzz9l9vXpcesnxGpJf1pfyuE="
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
5+
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DROPPED, '7761635', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events']);
6+
$wh->setRecipientEmail('foo@example.com');
7+
$wh->setTags(['my_tag_1', 'my_tag_2']);
8+
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10+
$wh->setReason('');
11+
12+
return $wh;

0 commit comments

Comments
 (0)
0