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

Skip to content

Commit 62642c6

Browse files
committed
[Webhook] Add Mailchimp webhook (fixes #50285)
1 parent df086fa commit 62642c6

25 files changed

+656
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
26012601

26022602
if ($webhookEnabled) {
26032603
$webhookRequestParsers = [
2604+
MailerBridge\Mailchimp\Webhook\MailchimpRequestParser::class => 'mailer.webhook.request_parser.mailchimp',
26042605
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
26052606
MailerBridge\Postmark\Webhook\PostmarkRequestParser::class => 'mailer.webhook.request_parser.postmark',
26062607
];

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

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

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Mailer\Bridge\Mailchimp\RemoteEvent\MailchimpPayloadConverter;
15+
use Symfony\Component\Mailer\Bridge\Mailchimp\Webhook\MailchimpRequestParser;
1416
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\MailgunPayloadConverter;
1517
use Symfony\Component\Mailer\Bridge\Mailgun\Webhook\MailgunRequestParser;
1618
use Symfony\Component\Mailer\Bridge\Postmark\RemoteEvent\PostmarkPayloadConverter;
@@ -27,5 +29,10 @@
2729
->set('mailer.webhook.request_parser.postmark', PostmarkRequestParser::class)
2830
->args([service('mailer.payload_converter.postmark')])
2931
->alias(PostmarkRequestParser::class, 'mailer.webhook.request_parser.postmark')
32+
33+
->set('mailer.payload_converter.mailchimp', MailchimpPayloadConverter::class)
34+
->set('mailer.webhook.request_parser.mailchimp', MailchimpRequestParser::class)
35+
->args([service('mailer.payload_converter.mailchimp'), param('webhook.routing.service'), param('webhook.routing.secret')])
36+
->alias(MailchimpRequestParser::class, 'mailer.webhook.request_parser.mailchimp')
3037
;
3138
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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', 'hard_bounce', 'soft_bounce', 'delivered', 'open', 'click', 'spam', 'unsub', 'reject'], true)) {
25+
throw new ParseException(sprintf('Unsupported event "%s".', $payload['event']));
26+
}
27+
if (\in_array($payload['event'], ['send', 'deferral', 'soft_bounce', 'hard_bounce', 'delivered', 'reject'], true)) {
28+
$name = match ($payload['event']) {
29+
'send' => MailerDeliveryEvent::RECEIVED,
30+
'deferral', 'soft_bounce' => MailerDeliveryEvent::DEFERRED,
31+
'hard_bounce' => MailerDeliveryEvent::BOUNCE,
32+
'delivered' => MailerDeliveryEvent::DELIVERED,
33+
'reject' => MailerDeliveryEvent::DROPPED,
34+
};
35+
36+
$event = new MailerDeliveryEvent($name, $payload['msg']['_id'], $payload);
37+
// reason is only available on failed messages
38+
$event->setReason($this->getReason($payload));
39+
} else {
40+
$name = match ($payload['event']) {
41+
'click' => MailerEngagementEvent::CLICK,
42+
'open' => MailerEngagementEvent::OPEN,
43+
'spam' => MailerEngagementEvent::SPAM,
44+
'unsub' => MailerEngagementEvent::UNSUBSCRIBE,
45+
};
46+
$event = new MailerEngagementEvent($name, $payload['msg']['_id'], $payload);
47+
}
48+
49+
if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['msg']['ts'])) {
50+
throw new ParseException(sprintf('Invalid date "%s".', $payload['msg']['ts']));
51+
}
52+
$event->setDate($date);
53+
$event->setRecipientEmail($payload['msg']['email']);
54+
$event->setMetadata($payload['msg']['metadata']);
55+
$event->setTags($payload['msg']['tags']);
56+
57+
return $event;
58+
}
59+
60+
private function getReason(array $payload): string
61+
{
62+
if (null !== $payload['msg']['diag']) {
63+
return $payload['msg']['diag'];
64+
}
65+
if (null !== $payload['msg']['bounce_description']) {
66+
return $payload['msg']['bounce_description'];
67+
}
68+
if (null !== $payload['msg']['smtp_events'] && null !== $payload['msg']['smtp_events']['diag']) {
69+
return $payload['msg']['smtp_events']['diag'];
70+
}
71+
return '';
72+
}
73+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
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