Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ed40f0

Browse files
committed
feature #53740 Mailersend webhook remote event (doobas, fabpot)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- Mailersend webhook remote event | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? |no | License | MIT Support webhooks from [MailerSend](https://www.mailersend.com/blog/monitor-email-events-using-webhooks) MailerSend developers [documentation](https://developers.mailersend.com/api/v1/webhooks.html#webhooks-overview) Commits ------- 0a62288 Mailersend webhook remote event
2 parents 83ec6fe + 0a62288 commit 6ed40f0

30 files changed

+909
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
25892589
if ($webhookEnabled) {
25902590
$webhookRequestParsers = [
25912591
MailerBridge\Brevo\Webhook\BrevoRequestParser::class => 'mailer.webhook.request_parser.brevo',
2592+
MailerBridge\MailerSend\Webhook\MailerSendRequestParser::class => 'mailer.webhook.request_parser.mailersend',
25922593
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
25932594
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
25942595
MailerBridge\Postmark\Webhook\PostmarkRequestParser::class => 'mailer.webhook.request_parser.postmark',

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\MailerSend\RemoteEvent\MailerSendPayloadConverter;
17+
use Symfony\Component\Mailer\Bridge\MailerSend\Webhook\MailerSendRequestParser;
1618
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\MailgunPayloadConverter;
1719
use Symfony\Component\Mailer\Bridge\Mailgun\Webhook\MailgunRequestParser;
1820
use Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent\MailjetPayloadConverter;
@@ -31,6 +33,11 @@
3133
->args([service('mailer.payload_converter.brevo')])
3234
->alias(BrevoRequestParser::class, 'mailer.webhook.request_parser.brevo')
3335

36+
->set('mailer.payload_converter.mailersend', MailerSendPayloadConverter::class)
37+
->set('mailer.webhook.request_parser.mailersend', MailerSendRequestParser::class)
38+
->args([service('mailer.payload_converter.mailersend')])
39+
->alias(MailerSendRequestParser::class, 'mailer.webhook.request_parser.mailersend')
40+
3441
->set('mailer.payload_converter.mailgun', MailgunPayloadConverter::class)
3542
->set('mailer.webhook.request_parser.mailgun', MailgunRequestParser::class)
3643
->args([service('mailer.payload_converter.mailgun')])

src/Symfony/Component/Mailer/Bridge/MailerSend/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.1
5+
---
6+
7+
* Add support for `RemoteEvent` and `Webhook`
8+
49
6.3
510
---
611

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\MailerSend\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+
/**
21+
* @author WoutervanderLoop.nl <[email protected]>
22+
*/
23+
final class MailerSendPayloadConverter implements PayloadConverterInterface
24+
{
25+
public function convert(array $payload): AbstractMailerEvent
26+
{
27+
if (\in_array($payload['type'], ['activity.sent', 'activity.delivered', 'activity.soft_bounced', 'activity.hard_bounced'], true)) {
28+
$name = match ($payload['type']) {
29+
'activity.sent' => MailerDeliveryEvent::RECEIVED,
30+
'activity.delivered' => MailerDeliveryEvent::DELIVERED,
31+
'activity.soft_bounced', 'activity.hard_bounced' => MailerDeliveryEvent::BOUNCE,
32+
};
33+
$event = new MailerDeliveryEvent($name, $this->getMessageId($payload), $payload);
34+
$event->setReason($this->getReason($payload));
35+
} else {
36+
$name = match ($payload['type']) {
37+
'activity.clicked', 'activity.clicked_unique' => MailerEngagementEvent::CLICK,
38+
'activity.unsubscribed' => MailerEngagementEvent::UNSUBSCRIBE,
39+
'activity.opened', 'activity.opened_unique' => MailerEngagementEvent::OPEN,
40+
'activity.spam_complaint' => MailerEngagementEvent::SPAM,
41+
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['type'])),
42+
};
43+
$event = new MailerEngagementEvent($name, $this->getMessageId($payload), $payload);
44+
}
45+
46+
if (!$date = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $payload['created_at'])) {
47+
throw new ParseException(sprintf('Invalid date "%s".', $payload['created_at']));
48+
}
49+
50+
$event->setDate($date);
51+
$event->setRecipientEmail($this->getRecipientEmail($payload));
52+
$event->setMetadata($this->getMetadata($payload));
53+
$event->setTags($this->getTags($payload));
54+
55+
return $event;
56+
}
57+
58+
private function getMessageId(array $payload): string
59+
{
60+
return $payload['data']['email']['message']['id'];
61+
}
62+
63+
private function getRecipientEmail(array $payload): string
64+
{
65+
return $payload['data']['email']['recipient']['email'];
66+
}
67+
68+
private function getReason(array $payload): string
69+
{
70+
if (isset($payload['data']['morph']['readable_reason'])) {
71+
return $payload['data']['morph']['readable_reason'];
72+
}
73+
74+
if (isset($payload['data']['morph']['reason'])) {
75+
return $payload['data']['morph']['reason'];
76+
}
77+
78+
return '';
79+
}
80+
81+
private function getTags(array $payload): array
82+
{
83+
return $payload['data']['email']['tags'] ?? [];
84+
}
85+
86+
private function getMetadata(array $payload): array
87+
{
88+
$morphObject = $payload['data']['morph']['object'] ?? null;
89+
90+
return match ($morphObject) {
91+
'open' => [
92+
'ip' => $payload['data']['morph']['ip'] ?? null
93+
],
94+
'click' => [
95+
'ip' => $payload['data']['morph']['ip'] ?? null,
96+
'url' => $payload['data']['morph']['url'] ?? null,
97+
],
98+
'recipient_unsubscribe' => [
99+
'reason' => $payload['data']['morph']['reason'] ?? null,
100+
'readable_reason' => $payload['data']['morph']['readable_reason'] ?? null,
101+
],
102+
default => [],
103+
};
104+
}
105+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"type": "activity.clicked",
3+
"domain_id": "7z3m5jgrogdpyo6n",
4+
"created_at": "2024-01-01T12:00:00.000000Z",
5+
"webhook_id": "7z3m5jgrogdpyo6n",
6+
"url": "https://www.mailersend.com/webhook",
7+
"data": {
8+
"object": "activity",
9+
"id": "62f114f8165fe0d8db0288e5",
10+
"type": "clicked",
11+
"created_at": "2024-01-01T12:00:00.000000Z",
12+
"email": {
13+
"object": "email",
14+
"id": "62f114f7165fe0d8db0288e2",
15+
"created_at": "2024-01-01T12:00:00.000000Z",
16+
"from": "[email protected]",
17+
"subject": "Test subject",
18+
"status": "delivered",
19+
"tags": ["test-tag"],
20+
"headers": null,
21+
"message": {
22+
"object": "message",
23+
"id": "62fb66bef54a112e920b5493",
24+
"created_at": "2024-01-01T12:00:00.000000Z"
25+
},
26+
"recipient": {
27+
"object": "recipient",
28+
"id": "62c69be104270ee9c0074d32",
29+
"email": "[email protected]",
30+
"created_at": "2024-01-01T12:00:00.000000Z"
31+
}
32+
},
33+
"morph": {
34+
"object": "click",
35+
"id": "62fb9215f2481f74e3085356",
36+
"created_at": "2024-01-01T12:00:00.000000Z",
37+
"ip": "127.0.0.1",
38+
"url": "https://www.mailersend.com"
39+
},
40+
"template_id": "0z76k5jg0o3yeg2d"
41+
}
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
6+
$wh->setRecipientEmail('[email protected]');
7+
$wh->setTags(["test-tag"]);
8+
$wh->setMetadata([
9+
'ip' => '127.0.0.1',
10+
'url' => 'https://www.mailersend.com'
11+
]);
12+
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));
13+
14+
return $wh;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"type": "activity.clicked_unique",
3+
"domain_id": "7z3m5jgrogdpyo6n",
4+
"created_at": "2024-01-01T12:00:00.000000Z",
5+
"webhook_id": "7z3m5jgrogdpyo6n",
6+
"url": "https://www.mailersend.com/webhook",
7+
"data": {
8+
"object": "activity",
9+
"id": "62f114f8165fe0d8db0288e5",
10+
"type": "clicked_unique",
11+
"created_at": "2024-01-01T12:00:00.000000Z",
12+
"email": {
13+
"object": "email",
14+
"id": "62f114f7165fe0d8db0288e2",
15+
"created_at": "2024-01-01T12:00:00.000000Z",
16+
"from": "[email protected]",
17+
"subject": "Test subject",
18+
"status": "delivered",
19+
"tags": ["test-tag"],
20+
"headers": null,
21+
"message": {
22+
"object": "message",
23+
"id": "62fb66bef54a112e920b5493",
24+
"created_at": "2024-01-01T12:00:00.000000Z"
25+
},
26+
"recipient": {
27+
"object": "recipient",
28+
"id": "62c69be104270ee9c0074d32",
29+
"email": "[email protected]",
30+
"created_at": "2024-01-01T12:00:00.000000Z"
31+
}
32+
},
33+
"morph": {
34+
"object": "click",
35+
"id": "62fb9215f2481f74e3085356",
36+
"created_at": "2024-01-01T12:00:00.000000Z",
37+
"ip": "127.0.0.1",
38+
"url": "https://www.mailersend.com"
39+
},
40+
"template_id": "0z76k5jg0o3yeg2d"
41+
}
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
6+
$wh->setRecipientEmail('[email protected]');
7+
$wh->setTags(["test-tag"]);
8+
$wh->setMetadata([
9+
'ip' => '127.0.0.1',
10+
'url' => 'https://www.mailersend.com'
11+
]);
12+
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));
13+
14+
return $wh;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"type": "activity.delivered",
3+
"domain_id": "7z3m5jgrogdpyo6n",
4+
"created_at": "2024-01-01T12:00:00.000000Z",
5+
"webhook_id": "7z3m5jgrogdpyo6n",
6+
"url": "https://www.mailersend.com/webhook",
7+
"data": {
8+
"object": "activity",
9+
"id": "62f114f8165fe0d8db0288e5",
10+
"type": "delivered",
11+
"created_at": "2024-01-01T12:00:00.000000Z",
12+
"email": {
13+
"object": "email",
14+
"id": "62f114f7165fe0d8db0288e2",
15+
"created_at": "2024-01-01T12:00:00.000000Z",
16+
"from": "[email protected]",
17+
"subject": "Test subject",
18+
"status": "delivered",
19+
"tags": ["test-tag"],
20+
"headers": null,
21+
"message": {
22+
"object": "message",
23+
"id": "62fb66bef54a112e920b5493",
24+
"created_at": "2024-01-01T12:00:00.000000Z"
25+
},
26+
"recipient": {
27+
"object": "recipient",
28+
"id": "62c69be104270ee9c0074d32",
29+
"email": "[email protected]",
30+
"created_at": "2024-01-01T12:00:00.000000Z"
31+
}
32+
},
33+
"morph": null,
34+
"template_id": "0z76k5jg0o3yeg2d"
35+
}
36+
}
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::DELIVERED, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
6+
$wh->setRecipientEmail('[email protected]');
7+
$wh->setTags(["test-tag"]);
8+
$wh->setMetadata([]);
9+
$wh->setReason('');
10+
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));
11+
12+
return $wh;

0 commit comments

Comments
 (0)