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

Skip to content

[Mailer][Webhook] Mailersend webhook remote event #53740

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
Feb 3, 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 @@ -2589,6 +2589,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
if ($webhookEnabled) {
$webhookRequestParsers = [
MailerBridge\Brevo\Webhook\BrevoRequestParser::class => 'mailer.webhook.request_parser.brevo',
MailerBridge\MailerSend\Webhook\MailerSendRequestParser::class => 'mailer.webhook.request_parser.mailersend',
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
MailerBridge\Postmark\Webhook\PostmarkRequestParser::class => 'mailer.webhook.request_parser.postmark',
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\MailerSend\RemoteEvent\MailerSendPayloadConverter;
use Symfony\Component\Mailer\Bridge\MailerSend\Webhook\MailerSendRequestParser;
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\MailgunPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailgun\Webhook\MailgunRequestParser;
use Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent\MailjetPayloadConverter;
Expand All @@ -31,6 +33,11 @@
->args([service('mailer.payload_converter.brevo')])
->alias(BrevoRequestParser::class, 'mailer.webhook.request_parser.brevo')

->set('mailer.payload_converter.mailersend', MailerSendPayloadConverter::class)
->set('mailer.webhook.request_parser.mailersend', MailerSendRequestParser::class)
->args([service('mailer.payload_converter.mailersend')])
->alias(MailerSendRequestParser::class, 'mailer.webhook.request_parser.mailersend')

->set('mailer.payload_converter.mailgun', MailgunPayloadConverter::class)
->set('mailer.webhook.request_parser.mailgun', MailgunRequestParser::class)
->args([service('mailer.payload_converter.mailgun')])
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MailerSend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add support for `RemoteEvent` and `Webhook`

6.3
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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\MailerSend\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;

/**
* @author WoutervanderLoop.nl <[email protected]>
*/
final class MailerSendPayloadConverter implements PayloadConverterInterface
{
public function convert(array $payload): AbstractMailerEvent
{
if (\in_array($payload['type'], ['activity.sent', 'activity.delivered', 'activity.soft_bounced', 'activity.hard_bounced'], true)) {
$name = match ($payload['type']) {
'activity.sent' => MailerDeliveryEvent::RECEIVED,
'activity.delivered' => MailerDeliveryEvent::DELIVERED,
'activity.soft_bounced', 'activity.hard_bounced' => MailerDeliveryEvent::BOUNCE,
};
$event = new MailerDeliveryEvent($name, $this->getMessageId($payload), $payload);
$event->setReason($this->getReason($payload));
} else {
$name = match ($payload['type']) {
'activity.clicked', 'activity.clicked_unique' => MailerEngagementEvent::CLICK,
'activity.unsubscribed' => MailerEngagementEvent::UNSUBSCRIBE,
'activity.opened', 'activity.opened_unique' => MailerEngagementEvent::OPEN,
'activity.spam_complaint' => MailerEngagementEvent::SPAM,
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['type'])),
};
$event = new MailerEngagementEvent($name, $this->getMessageId($payload), $payload);
}

if (!$date = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $payload['created_at'])) {
throw new ParseException(sprintf('Invalid date "%s".', $payload['created_at']));
}

$event->setDate($date);
$event->setRecipientEmail($this->getRecipientEmail($payload));
$event->setMetadata($this->getMetadata($payload));
$event->setTags($this->getTags($payload));

return $event;
}

private function getMessageId(array $payload): string
{
return $payload['data']['email']['message']['id'];
}

private function getRecipientEmail(array $payload): string
{
return $payload['data']['email']['recipient']['email'];
}

private function getReason(array $payload): string
{
if (isset($payload['data']['morph']['readable_reason'])) {
return $payload['data']['morph']['readable_reason'];
}

if (isset($payload['data']['morph']['reason'])) {
return $payload['data']['morph']['reason'];
}

return '';
}

private function getTags(array $payload): array
{
return $payload['data']['email']['tags'] ?? [];
}

private function getMetadata(array $payload): array
{
$morphObject = $payload['data']['morph']['object'] ?? null;

return match ($morphObject) {
'open' => [
'ip' => $payload['data']['morph']['ip'] ?? null
],
'click' => [
'ip' => $payload['data']['morph']['ip'] ?? null,
'url' => $payload['data']['morph']['url'] ?? null,
],
'recipient_unsubscribe' => [
'reason' => $payload['data']['morph']['reason'] ?? null,
'readable_reason' => $payload['data']['morph']['readable_reason'] ?? null,
],
default => [],
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"type": "activity.clicked",
"domain_id": "7z3m5jgrogdpyo6n",
"created_at": "2024-01-01T12:00:00.000000Z",
"webhook_id": "7z3m5jgrogdpyo6n",
"url": "https://www.mailersend.com/webhook",
"data": {
"object": "activity",
"id": "62f114f8165fe0d8db0288e5",
"type": "clicked",
"created_at": "2024-01-01T12:00:00.000000Z",
"email": {
"object": "email",
"id": "62f114f7165fe0d8db0288e2",
"created_at": "2024-01-01T12:00:00.000000Z",
"from": "[email protected]",
"subject": "Test subject",
"status": "delivered",
"tags": ["test-tag"],
"headers": null,
"message": {
"object": "message",
"id": "62fb66bef54a112e920b5493",
"created_at": "2024-01-01T12:00:00.000000Z"
},
"recipient": {
"object": "recipient",
"id": "62c69be104270ee9c0074d32",
"email": "[email protected]",
"created_at": "2024-01-01T12:00:00.000000Z"
}
},
"morph": {
"object": "click",
"id": "62fb9215f2481f74e3085356",
"created_at": "2024-01-01T12:00:00.000000Z",
"ip": "127.0.0.1",
"url": "https://www.mailersend.com"
},
"template_id": "0z76k5jg0o3yeg2d"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
$wh->setRecipientEmail('[email protected]');
$wh->setTags(["test-tag"]);
$wh->setMetadata([
'ip' => '127.0.0.1',
'url' => 'https://www.mailersend.com'
]);
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"type": "activity.clicked_unique",
"domain_id": "7z3m5jgrogdpyo6n",
"created_at": "2024-01-01T12:00:00.000000Z",
"webhook_id": "7z3m5jgrogdpyo6n",
"url": "https://www.mailersend.com/webhook",
"data": {
"object": "activity",
"id": "62f114f8165fe0d8db0288e5",
"type": "clicked_unique",
"created_at": "2024-01-01T12:00:00.000000Z",
"email": {
"object": "email",
"id": "62f114f7165fe0d8db0288e2",
"created_at": "2024-01-01T12:00:00.000000Z",
"from": "[email protected]",
"subject": "Test subject",
"status": "delivered",
"tags": ["test-tag"],
"headers": null,
"message": {
"object": "message",
"id": "62fb66bef54a112e920b5493",
"created_at": "2024-01-01T12:00:00.000000Z"
},
"recipient": {
"object": "recipient",
"id": "62c69be104270ee9c0074d32",
"email": "[email protected]",
"created_at": "2024-01-01T12:00:00.000000Z"
}
},
"morph": {
"object": "click",
"id": "62fb9215f2481f74e3085356",
"created_at": "2024-01-01T12:00:00.000000Z",
"ip": "127.0.0.1",
"url": "https://www.mailersend.com"
},
"template_id": "0z76k5jg0o3yeg2d"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
$wh->setRecipientEmail('[email protected]');
$wh->setTags(["test-tag"]);
$wh->setMetadata([
'ip' => '127.0.0.1',
'url' => 'https://www.mailersend.com'
]);
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "activity.delivered",
"domain_id": "7z3m5jgrogdpyo6n",
"created_at": "2024-01-01T12:00:00.000000Z",
"webhook_id": "7z3m5jgrogdpyo6n",
"url": "https://www.mailersend.com/webhook",
"data": {
"object": "activity",
"id": "62f114f8165fe0d8db0288e5",
"type": "delivered",
"created_at": "2024-01-01T12:00:00.000000Z",
"email": {
"object": "email",
"id": "62f114f7165fe0d8db0288e2",
"created_at": "2024-01-01T12:00:00.000000Z",
"from": "[email protected]",
"subject": "Test subject",
"status": "delivered",
"tags": ["test-tag"],
"headers": null,
"message": {
"object": "message",
"id": "62fb66bef54a112e920b5493",
"created_at": "2024-01-01T12:00:00.000000Z"
},
"recipient": {
"object": "recipient",
"id": "62c69be104270ee9c0074d32",
"email": "[email protected]",
"created_at": "2024-01-01T12:00:00.000000Z"
}
},
"morph": null,
"template_id": "0z76k5jg0o3yeg2d"
}
}
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::DELIVERED, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
$wh->setRecipientEmail('[email protected]');
$wh->setTags(["test-tag"]);
$wh->setMetadata([]);
$wh->setReason('');
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"type": "activity.hard_bounced",
"domain_id": "7z3m5jgrogdpyo6n",
"created_at": "2024-01-01T12:00:00.000000Z",
"webhook_id": "7z3m5jgrogdpyo6n",
"url": "https://www.mailersend.com/webhook",
"data": {
"object": "activity",
"id": "62f114f8165fe0d8db0288e5",
"type": "hard_bounced",
"created_at": "2024-01-01T12:00:00.000000Z",
"email": {
"object": "email",
"id": "62f114f7165fe0d8db0288e2",
"created_at": "2024-01-01T12:00:00.000000Z",
"from": "[email protected]",
"subject": "Test subject",
"status": "rejected",
"tags": ["test-tag"],
"headers": null,
"message": {
"object": "message",
"id": "62fb66bef54a112e920b5493",
"created_at": "2024-01-01T12:00:00.000000Z"
},
"recipient": {
"object": "recipient",
"id": "62c69be104270ee9c0074d32",
"email": "[email protected]",
"created_at": "2024-01-01T12:00:00.000000Z"
}
},
"morph": {
"object": "recipient_bounce",
"reason": "Host or domain name not found"
},
"template_id": "0z76k5jg0o3yeg2d"
}
}
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::BOUNCE, '62fb66bef54a112e920b5493', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true));
$wh->setRecipientEmail('[email protected]');
$wh->setTags(["test-tag"]);
$wh->setMetadata([]);
$wh->setReason('Host or domain name not found');
$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2024-01-01T12:00:00.000000Z'));

return $wh;
Loading