Skip to content

Commit

Permalink
[Webhook] Add Mailchimp webhook (fixes symfony#50285)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohJohan committed Oct 1, 2024
1 parent 9a3e33b commit 43387e5
Show file tree
Hide file tree
Showing 28 changed files with 801 additions and 1 deletion.
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')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 (!empty($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": "[email protected]",
"sender": "[email protected]",
"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": "[email protected]",
"sender": "[email protected]",
"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('[email protected]');
$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('[email protected]');
$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": "[email protected]",
"sender": "[email protected]",
"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('[email protected]');
$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": "[email protected]",
"sender": "[email protected]",
"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('[email protected]');
$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": "[email protected]",
"sender": "[email protected]",
"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('[email protected]');
$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": "hard_bounce",
"msg": {
"ts": 1365109999,
"subject": "Foo bar",
"email": "[email protected]",
"sender": "[email protected]",
"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": "7761633",
"_version": "123",
"subaccount": null,
"diag": null,
"bounce_description": "hard bounce",
"template": null
}
}
],
"X-Mandrill-Signature": "ip7K8yTxSFakEhIGiJz8tydjVsQ="
}
Loading

0 comments on commit 43387e5

Please sign in to comment.