Skip to content

[Webhook] Add Mailchimp webhook #50324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
$webhookRequestParsers = [
MailerBridge\Brevo\Webhook\BrevoRequestParser::class => 'mailer.webhook.request_parser.brevo',
MailerBridge\MailerSend\Webhook\MailerSendRequestParser::class => 'mailer.webhook.request_parser.mailersend',
MailerBridge\Mailchimp\Webhook\MailchimpRequestParser::class => 'mailer.webhook.request_parser.mailchimp',
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
MailerBridge\Mailomat\Webhook\MailomatRequestParser::class => 'mailer.webhook.request_parser.mailomat',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Mailer\Bridge\Brevo\RemoteEvent\BrevoPayloadConverter;
use Symfony\Component\Mailer\Bridge\Brevo\Webhook\BrevoRequestParser;
use Symfony\Component\Mailer\Bridge\Mailchimp\RemoteEvent\MailchimpPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailchimp\Webhook\MailchimpRequestParser;
use Symfony\Component\Mailer\Bridge\MailerSend\RemoteEvent\MailerSendPayloadConverter;
use Symfony\Component\Mailer\Bridge\MailerSend\Webhook\MailerSendRequestParser;
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\MailgunPayloadConverter;
Expand Down Expand Up @@ -83,5 +85,10 @@
->set('mailer.webhook.request_parser.sweego', SweegoRequestParser::class)
->args([service('mailer.payload_converter.sweego')])
->alias(SweegoRequestParser::class, 'mailer.webhook.request_parser.sweego')

->set('mailer.payload_converter.mailchimp', MailchimpPayloadConverter::class)
->set('mailer.webhook.request_parser.mailchimp', MailchimpRequestParser::class)
->args([service('mailer.payload_converter.mailchimp')])
->alias(MailchimpRequestParser::class, 'mailer.webhook.request_parser.mailchimp')
;
};
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailchimp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add support for webhook

4.4.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Mailchimp\RemoteEvent;

use Symfony\Component\RemoteEvent\Event\Mailer\AbstractMailerEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
use Symfony\Component\RemoteEvent\Exception\ParseException;
use Symfony\Component\RemoteEvent\PayloadConverterInterface;

final class MailchimpPayloadConverter implements PayloadConverterInterface
{
public function convert(array $payload): AbstractMailerEvent
{
if (\in_array($payload['event'], ['send', 'deferral', 'soft_bounce', 'hard_bounce', 'delivered', 'reject'], true)) {
$name = match ($payload['event']) {
'send' => MailerDeliveryEvent::RECEIVED,
'deferral', => MailerDeliveryEvent::DEFERRED,
'soft_bounce', 'hard_bounce' => MailerDeliveryEvent::BOUNCE,
'delivered' => MailerDeliveryEvent::DELIVERED,
'reject' => MailerDeliveryEvent::DROPPED,
};

$event = new MailerDeliveryEvent($name, $payload['msg']['_id'], $payload);
// reason is only available on failed messages
$event->setReason($this->getReason($payload));
} else {
$name = match ($payload['event']) {
'click' => MailerEngagementEvent::CLICK,
'open' => MailerEngagementEvent::OPEN,
'spam' => MailerEngagementEvent::SPAM,
'unsub' => MailerEngagementEvent::UNSUBSCRIBE,
default => throw new ParseException(\sprintf('Unsupported event "%s".', $payload['event'])),
};
$event = new MailerEngagementEvent($name, $payload['msg']['_id'], $payload);
}

if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['msg']['ts'])) {
throw new ParseException(\sprintf('Invalid date "%s".', $payload['msg']['ts']));
}
$event->setDate($date);
$event->setRecipientEmail($payload['msg']['email']);
$event->setMetadata($payload['msg']['metadata']);
$event->setTags($payload['msg']['tags']);

return $event;
}

private function getReason(array $payload): string
{
if (null !== $payload['msg']['diag']) {
return $payload['msg']['diag'];
}
if (null !== $payload['msg']['bounce_description']) {
return $payload['msg']['bounce_description'];
}

if (null !== $payload['msg']['smtp_events'] && [] !== $payload['msg']['smtp_events']) {
$reasons = [];
foreach ($payload['msg']['smtp_events'] as $event) {
$reasons[] = \sprintf('type: %s diag: %s', $event['type'], $event['diag']);
}

// Return concatenated reasons or an empty string if no reasons found
return implode(' ', $reasons);
}

return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"mandrill_events": [
{
"ts": 1365109999,
"event": "click",
"msg": {
"ts": 1365109999,
"subject": "Foo bar",
"email": "foo@example.com",
"sender": "bar@example.com",
"tags": [
"my_tag_1",
"my_tag_2"
],
"smtp_events": null,
"opens": [
{
"ts": 1365110000
}
],
"clicks": [
{
"ts": 1365110000,
"url": "https://www.example.com"
}
],
"state": "sent",
"metadata": {
"mandrill-var-1": "foo",
"mandrill-var-2": "bar"
},
"_id": "7761630",
"_version": "123",
"subaccount": null,
"diag": null,
"bounce_description": null,
"template": null
}
},
{
"ts": 1365109999,
"event": "deferral",
"msg": {
"ts": 1365109999,
"subject": "Foo bar",
"email": "foo@example.com",
"sender": "bar@example.com",
"tags": [
"my_tag_1",
"my_tag_2"
],
"smtp_events": null,
"opens": null,
"clicks": null,
"state": "sent",
"metadata": {
"mandrill-var-1": "foo",
"mandrill-var-2": "bar"
},
"_id": "7761631",
"_version": "123",
"subaccount": null,
"diag": null,
"bounce_description": null,
"template": null
}
}
],
"X-Mandrill-Signature": "Mjask0i0giC/nEAYZrt6sX6JF2M="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

$wh1 = new MailerEngagementEvent(
MailerEngagementEvent::CLICK, '7761630', json_decode(
file_get_contents(
str_replace('.php', '.json', __FILE__)
), true, flags: JSON_THROW_ON_ERROR
)['mandrill_events'][0]
);
$wh1->setRecipientEmail('foo@example.com');
$wh1->setTags(['my_tag_1', 'my_tag_2']);
$wh1->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
$wh1->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));

$wh2 = new MailerDeliveryEvent(
MailerDeliveryEvent::DEFERRED, '7761631', json_decode(
file_get_contents(
str_replace('.php', '.json', __FILE__)
), true, flags: JSON_THROW_ON_ERROR
)['mandrill_events'][1]
);
$wh2->setRecipientEmail('foo@example.com');
$wh2->setTags(['my_tag_1', 'my_tag_2']);
$wh2->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
$wh2->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
$wh2->setReason('');

return [$wh1, $wh2];
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"mandrill_events": [
{
"ts": 1365109999,
"event": "click",
"msg": {
"ts": 1365109999,
"subject": "Foo bar",
"email": "foo@example.com",
"sender": "bar@example.com",
"tags": [
"my_tag_1",
"my_tag_2"
],
"smtp_events": null,
"opens": [
{
"ts": 1365110000
}
],
"clicks": [
{
"ts": 1365110000,
"url": "https://www.example.com"
}
],
"state": "sent",
"metadata": {
"mandrill-var-1": "foo",
"mandrill-var-2": "bar"
},
"_id": "7761630",
"_version": "123",
"subaccount": null,
"diag": null,
"bounce_description": null,
"template": null
}
}
],
"X-Mandrill-Signature": "5mcP4EaDf9cd1tFLAIY+E13Eqmw="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

$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]);
$wh->setRecipientEmail('foo@example.com');
$wh->setTags(['my_tag_1', 'my_tag_2']);
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"mandrill_events": [
{
"ts": 1365109999,
"event": "deferral",
"msg": {
"ts": 1365109999,
"subject": "Foo bar",
"email": "foo@example.com",
"sender": "bar@example.com",
"tags": [
"my_tag_1",
"my_tag_2"
],
"smtp_events": null,
"opens": null,
"clicks": null,
"state": "sent",
"metadata": {
"mandrill-var-1": "foo",
"mandrill-var-2": "bar"
},
"_id": "7761631",
"_version": "123",
"subaccount": null,
"diag": null,
"bounce_description": null,
"template": null
}
}
],
"X-Mandrill-Signature": "VuT14QGTsui1T7B+FqjV6SqACDA="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;

$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]);
$wh->setRecipientEmail('foo@example.com');
$wh->setTags(['my_tag_1', 'my_tag_2']);
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));
$wh->setReason('');

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"mandrill_events": [
{
"ts": 1365109999,
"event": "delivered",
"msg": {
"ts": 1365109999,
"subject": "Foo bar",
"email": "foo@example.com",
"sender": "bar@example.com",
"tags": [
"my_tag_1",
"my_tag_2"
],
"smtp_events": null,
"opens": null,
"clicks": null,
"state": "sent",
"metadata": {
"mandrill-var-1": "foo",
"mandrill-var-2": "bar"
},
"_id": "7761632",
"_version": "123",
"subaccount": null,
"diag": null,
"bounce_description": null,
"template": null
}
}
],
"X-Mandrill-Signature": "Q287c9JdfPkKnVE9vRJw5jd+XcE="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;

$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]);
$wh->setRecipientEmail('foo@example.com');
$wh->setTags(['my_tag_1', 'my_tag_2']);
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999));

return [$wh];
Loading