8000 [Mailer][Webhook] Add MailerSend support by doobas · Pull Request #53583 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer][Webhook] Add MailerSend support #53583

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added support for RemoteEvent and Webhook
  • Loading branch information
doobas committed Jan 19, 2024
commit 7dbf4c73e6cdae797c282dfb93c75b33a5c8fba6
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
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 <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\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 <info@woutervanderloop.nl>
*/
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": "test@mailersend.com",
"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": "test@example.com",
"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('test@example.com');
$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": "test@mailersend.com",
"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": "test@example.com",
"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('test@example.com');
$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": "test@mailersend.com",
"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": "test@example.com",
"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('test@example.com');
$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;
C24
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": "test@mailersend.com",
"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": "test@example.com",
"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('test@example.com');
$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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"type": "activity.opened",
"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": "opened",
"created_at": "2024-01-01T12:00:00.000000Z",
"email": {
"object": "email",
"id": "62f114f7165fe0d8db0288e2",
"created_at": "2024-01-01T12:00:00.000000Z",
"from": "test@mailersend.com",
"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": "test@example.com",
"created_at": "2024-01-01T12:00:00.000000Z"
}
},
"morph": {
"object": "open",
"id": "62fb9151f2481f74e3085352",
"created_at": "2024-01-01T12:00:00.000000Z",
"ip": "127.0.0.1"
},
"template_id": "0z76k5jg0o3yeg2d"
}
}
Loading
0