Skip to content

[Mailer][Webhook] Mailtrap webhook support #58403

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
Sep 30, 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 @@ -2681,6 +2681,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
MailerBridge\Mailomat\Webhook\MailomatRequestParser::class => 'mailer.webhook.request_parser.mailomat',
MailerBridge\Postmark\Webhook\PostmarkRequestParser::class => 'mailer.webhook.request_parser.postmark',
MailerBridge\Mailtrap\Webhook\MailtrapRequestParser::class => 'mailer.webhook.request_parser.mailtrap',
MailerBridge\Resend\Webhook\ResendRequestParser::class => 'mailer.webhook.request_parser.resend',
MailerBridge\Sendgrid\Webhook\SendgridRequestParser::class => 'mailer.webhook.request_parser.sendgrid',
MailerBridge\Sweego\Webhook\SweegoRequestParser::class => 'mailer.webhook.request_parser.sweego',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\Mailer\Bridge\Mailjet\Webhook\MailjetRequestParser;
use Symfony\Component\Mailer\Bridge\Mailomat\RemoteEvent\MailomatPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailomat\Webhook\MailomatRequestParser;
use Symfony\Component\Mailer\Bridge\Mailtrap\RemoteEvent\MailtrapPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailtrap\Webhook\MailtrapRequestParser;
use Symfony\Component\Mailer\Bridge\Postmark\RemoteEvent\PostmarkPayloadConverter;
use Symfony\Component\Mailer\Bridge\Postmark\Webhook\PostmarkRequestParser;
use Symfony\Component\Mailer\Bridge\Resend\RemoteEvent\ResendPayloadConverter;
Expand Down Expand Up @@ -62,6 +64,11 @@
->args([service('mailer.payload_converter.postmark')])
->alias(PostmarkRequestParser::class, 'mailer.webhook.request_parser.postmark')

->set('mailer.payload_converter.mailtrap', MailtrapPayloadConverter::class)
->set('mailer.webhook.request_parser.mailtrap', MailtrapRequestParser::class)
->args([service('mailer.payload_converter.mailtrap')])
->alias(MailtrapRequestParser::class, 'mailer.webhook.request_parser.mailtrap')

->set('mailer.payload_converter.resend', ResendPayloadConverter::class)
->set('mailer.webhook.request_parser.resend', ResendRequestParser::class)
->args([service('mailer.payload_converter.resend')])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Mailtrap\RemoteEvent;

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;
use Symfony\Component\RemoteEvent\RemoteEvent;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class MailtrapPayloadConverter implements PayloadConverterInterface
{
public function convert(array $payload): RemoteEvent
{
$type = match ($payload['event']) {
'delivery' => MailerDeliveryEvent::DELIVERED,
'open' => MailerEngagementEvent::OPEN,
'click' => MailerEngagementEvent::CLICK,
'unsubscribe' => MailerEngagementEvent::UNSUBSCRIBE,
'spam' => MailerEngagementEvent::SPAM,
'soft bounce', 'bounce' => MailerDeliveryEvent::BOUNCE,
'suspension', 'reject' => MailerDeliveryEvent::DROPPED,
default => throw new ParseException(\sprintf('Unsupported event "%s".', $payload['event'])),
};

if (\in_array($type, [MailerDeliveryEvent::DELIVERED, MailerDeliveryEvent::BOUNCE, MailerDeliveryEvent::DROPPED], true)) {
$event = new MailerDeliveryEvent($type, $payload['message_id'], $payload);
$event->setReason($payload['reason'] ?? $payload['response'] ?? '');
} else {
$event = new MailerEngagementEvent($type, $payload['message_id'], $payload);
}

if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['timestamp'])) {
throw new ParseException(\sprintf('Invalid date "%s".', $payload['timestamp']));
}

$event->setDate($date);
$event->setRecipientEmail($payload['email']);

if (isset($payload['category'])) {
$event->setTags([$payload['category']]);
}

if (isset($payload['custom_variables'])) {
$event->setMetadata($payload['custom_variables']);
}

return $event;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"events": [
{
"event": "bounce",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000001",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"response": "[CS01] Message rejected due to local policy",
"response_code": 555,
"bounce_category": "spam"
},
{
"event": "click",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000002",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"ip": "142.86.27.2",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"url": "https://mailtrap.io/email-api"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

$wh1 = new MailerDeliveryEvent(MailerDeliveryEvent::BOUNCE, '00000000-0000-0000-0000-000000000001', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh1->setRecipientEmail('receiver@example.com');
$wh1->setTags(['Password reset']);
$wh1->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh1->setReason('[CS01] Message rejected due to local policy');
$wh1->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

$wh2 = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '00000000-0000-0000-0000-000000000002', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][1]);
$wh2->setRecipientEmail('receiver@example.com');
$wh2->setTags(['Password reset']);
$wh2->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh2->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh1, $wh2];
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"events": [
{
"event": "bounce",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"response": "[CS01] Message rejected due to local policy",
"response_code": 555,
"bounce_category": "spam"
}
]
}
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, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setReason('[CS01] Message rejected due to local policy');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"events": [
{
"event": "click",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"ip": "142.86.27.2",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"url": "https://mailtrap.io/email-api"
}
]
}
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, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"events": [
{
"event": "delivery",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc"
}
]
}
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, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setReason('');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"events": [
{
"event": "open",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"ip": "127.138.158.185",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
]
}
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::OPEN, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"events": [
{
"event": "reject",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"reason": "Recipient in suppression list. Reason: unsubscription"
}
]
}
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::DROPPED, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setReason('Recipient in suppression list. Reason: unsubscription');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"events": [
{
"event": "soft bounce",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"response": "4.7.1 Temporary error, please retry",
"response_code": 451,
"bounce_category": "greylisting"
}
]
}
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, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setReason('4.7.1 Temporary error, please retry');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"events": [
{
"event": "spam",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc"
}
]
}
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::SPAM, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"events": [
{
"event": "suspension",
"category": "Password reset",
"custom_variables": {
"variable_a": "value",
"variable_b": "value2"
},
"message_id": "00000000-0000-0000-0000-000000000000",
"email": "receiver@example.com",
"timestamp": 1726358034,
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
"reason": "Your account has reached its daily sending limit."
}
]
}
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::DROPPED, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
$wh->setRecipientEmail('receiver@example.com');
$wh->setTags(['Password reset']);
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
$wh->setReason('Your account has reached its daily sending limit.');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));

return [$wh];
Loading
Loading