8000 feature #50324 [Webhook] Add Mailchimp webhook (johanadivare) · symfony/symfony@8b2ffdd · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 8b2ffdd

Browse files
committed
feature #50324 [Webhook] Add Mailchimp webhook (johanadivare)
This PR was merged into the 7.2 branch. Discussion ---------- [Webhook] Add Mailchimp webhook | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #50285 | License | MIT | Doc PR | symfony/symfony-docs#20286 This will add Mailchimp webhook see https://mailchimp.com/developer/transactional/docs/webhooks/ Commits ------- b75615f [Webhook] Add Mailchimp webhook (fixes #50285)
2 parents 7431ef8 + b75615f commit 8b2ffdd

29 files changed

+804
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
26772677
$webhookRequestParsers = [
26782678
MailerBridge\Brevo\Webhook\BrevoRequestParser::class => 'mailer.webhook.request_parser.brevo',
26792679
MailerBridge\MailerSend\Webhook\MailerSendRequestParser::class => 'mailer.webhook.request_parser.mailersend',
2680+
MailerBridge\Mailchimp\Webhook\MailchimpRequestParser::class => 'mailer.webhook.request_parser.mailchimp',
26802681
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
26812682
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
26822683
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;
@@ -83,5 +85,10 @@
8385
->set('mailer.webhook.request_parser.sweego', SweegoRequestParser::class)
8486
->args([service('mailer.payload_converter.sweego')])
8587
->alias(SweegoRequestParser::class, 'mailer.webhook.request_parser.sweego')
88+
89+
->set('mailer.payload_converter.mailchimp', MailchimpPayloadConverter::class)
90+
->set('mailer.webhook.request_parser.mailchimp', MailchimpRequestParser::class)
91+
->args([service('mailer.payload_converter.mailchimp')])
92+
->alias(MailchimpRequestParser::class, 'mailer.webhook.request_parser.mailchimp')
8693
;
8794
};

src/Symfony/Component/Mailer/Bridge/Mailchimp/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add support for webhook
8+
49
4.4.0
510
-----
611

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
'reject' => 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+
67+
if (null !== $payload['msg']['smtp_events'] && [] !== $payload['msg']['smtp_events']) {
68+
$reasons = [];
69+
foreach ($payload['msg']['smtp_events'] as $event) {
70+
$reasons[] = \sprintf('type: %s diag: %s', $event['type'], $event['diag']);
71+
}
72+
73+
// Return concatenated reasons or an empty string if no reasons found
74+
return implode(' ', $reasons);
75+
}
76+
77+
return '';
78+
}
79+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"mandrill_events": [
3+
{
4+
"ts": 1365109999,
5+
"event": "click",
6+
"msg": {
7+
"ts": 1365109999,
8+
"subject": "Foo bar",
9+
"email": "foo@example.com",
10+
"sender": "bar@example.com",
11+
"tags": [
12+
"my_tag_1",
13+
"my_tag_2"
14+
],
15+
"smtp_events": null,
16+
"opens": [
17+
{
18+
"ts": 1365110000
19+
}
20+
],
21+
"clicks": [
22+
{
23+
"ts": 1365110000,
24+
"url": "https://www.example.com"
25+
}
26+
],
27+
"state": "sent",
28+
"metadata": {
29+
"mandrill-var-1": "foo",
30+
"mandrill-var-2": "bar"
31+
},
32+
"_id": "7761630",
33+
"_version": "123",
34+
"subaccount": null,
35+
"diag": null,
36+
"bounce_description": null,
37+
"template": null
38+
}
39+
},
40+
{
41+
"ts": 1365109999,
42+
"event": "deferral",
43+
"msg": {
44+
"ts": 1365109999,
45+
"subject": "Foo bar",
46+
"email": "foo@example.com",
47+
"sender": "bar@example.com",
48+
"tags": [
49+
"my_tag_1",
50+
"my_tag_2"
51+
],
52+
"smtp_events": null,
53+
"opens": null,
54+
"clicks": null,
55+
"state": "sent",
56+
"metadata": {
57+
"mandrill-var-1": "foo",
58+
"mandrill-var-2": "bar"
59+
},
60+
"_id": "7761631",
61+
"_version": "123",
62+
"subaccount": null,
63+
"diag": null,
64+
"bounce_description": null,
65+
"template": null
66+
}
67+
}
68+
],
69+
"X-Mandrill-Signature": "Mjask0i0giC/nEAYZrt6sX6JF2M="
70+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
5+
6+
$wh1 = new MailerEngagementEvent(
7+
MailerEngagementEvent::CLICK, '7761630', json_decode(
8+
file_get_contents(
9+
str_replace('.php', '.json', __FILE__)
10+
), true, flags: JSON_THROW_ON_ERROR
11+
)['mandrill_events'][0]
12+
);
13+
$wh1->setRecipientEmail('foo@example.com');
14+
$wh1->setTags(['my_tag_1', 'my_tag_2']);
15+
$wh1->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
16+
$wh1->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
17+
18+
$wh2 = new MailerDeliveryEvent(
19+
MailerDeliveryEvent::DEFERRED, '7761631', json_decode(
20+
file_get_contents(
21+
str_replace('.php', '.json', __FILE__)
22+
), true, flags: JSON_THROW_ON_ERROR
23+
)['mandrill_events'][1]
24+
);
25+
$wh2->setRecipientEmail('foo@example.com');
26+
$wh2->setTags(['my_tag_1', 'my_tag_2']);
27+
$wh2->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
28+
$wh2->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
10000
29+
$wh2->setReason('');
30+
31+
return [$wh1, $wh2];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"mandrill_events": [
3+
{
4+
"ts": 1365109999,
5+
"event": "click",
6+
"msg": {
7+
"ts": 1365109999,
8+
"subject": "Foo bar",
9+
"email": "foo@example.com",
10+
"sender": "bar@example.com",
11+
"tags": [
12+
"my_tag_1",
13+
"my_tag_2"
14+
],
15+
"smtp_events": null,
16+
"opens": [
17+
{
18+
"ts": 1365110000
19+
}
20+
],
21+
"clicks": [
22+
{
23+
"ts": 1365110000,
24+
"url": "https://www.example.com"
25+
}
26+
],
27+
"state": "sent",
28+
"metadata": {
29+
"mandrill-var-1": "foo",
30+
"mandrill-var-2": "bar"
31+
},
32+
"_id": "7761630",
33+
"_version": "123",
34+
"subaccount": null,
35+
"diag": null,
36+
"bounce_description": null,
37+
"template": null
38+
}
39+
}
40+
],
41+
"X-Mandrill-Signature": "5mcP4EaDf9cd1tFLAIY+E13Eqmw="
42+
}
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'][0]);
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"mandrill_events": [
3+
{
4+
"ts": 1365109999,
5+
"event": "deferral",
6+
"msg": {
7+
"ts": 1365109999,
8+
"subject": "Foo bar",
9+
"email": "foo@example.com",
10+
"sender": "bar@example.com",
11+
"tags": [
12+
"my_tag_1",
13+
"my_tag_2"
14+
],
15+
"smtp_events": null,
16+
"opens": null,
17+
"clicks": null,
18+
"state": "sent",
19+
"metadata": {
20+
"mandrill-var-1": "foo",
21+
"mandrill-var-2": "bar"
22+
},
23+
"_id": "7761631",
24+
"_version": "123",
25+
"subaccount": null,
26+
"diag": null,
27+
"bounce_description": null,
28+
"template": null
29+
}
30+
}
31+
],
32+
"X-Mandrill-Signature": "VuT14QGTsui1T7B+FqjV6SqACDA="
33+
}
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'][0]);
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"mandrill_events": [
3+
{
4+
"ts": 1365109999,
5+
"event": "delivered",
6+
"msg": {
7+
"ts": 1365109999,
8+
"subject": "Foo bar",
9+
"email": "foo@example.com",
10+
"sender": "bar@example.com",
11+
"tags": [
12+
"my_tag_1",
13+
"my_tag_2"
14+
],
15+
"smtp_events": null,
16+
"opens": null,
17+
"clicks": null,
18+
"state": "sent",
19+
"metadata": {
20+
"mandrill-var-1": "foo",
21+
"mandrill-var-2": "bar"
22+
},
23+
"_id": "7761632",
24+
"_version": "123",
25+
"subaccount": null,
26+
"diag": null,
27+
"bounce_description": null,
28+
"template": null
29+
}
30+
}
31+
],
32+
"X-Mandrill-Signature": "Q287c9JdfPkKnVE9vRJw5jd+XcE="
33+
}
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::DELIVERED, '7761632', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events'][0]);
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];

0 commit comments

Comments
 (0)
0