8000 feature #58403 [Mailer][Webhook] Mailtrap webhook support (kbond) · symfony/symfony@87ac37b · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 87ac37b

Browse files
committed
feature #58403 [Mailer][Webhook] Mailtrap webhook support (kbond)
This PR was merged into the 7.2 branch. Discussion ---------- [Mailer][Webhook] Mailtrap webhook support | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | n/a | License | MIT Commits ------- c79a984 [Mailer] add Mailtrap webhook support
2 parents 10ac244 + c79a984 commit 87ac37b

26 files changed

+474
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,6 +2681,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
26812681
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
26822682
MailerBridge\Mailomat\Webhook\MailomatRequestParser::class => 'mailer.webhook.request_parser.mailomat',
26832683
MailerBridge\Postmark\Webhook\PostmarkRequestParser::class => 'mailer.webhook.request_parser.postmark',
2684+
MailerBridge\Mailtrap\Webhook\MailtrapRequestParser::class => 'mailer.webhook.request_parser.mailtrap',
26842685
MailerBridge\Resend\Webhook\ResendRequestParser::class => 'mailer.webhook.request_parser.resend',
26852686
MailerBridge\Sendgrid\Webhook\SendgridRequestParser::class => 'mailer.webhook.request_parser.sendgrid',
26862687
MailerBridge\Sweego\Webhook\SweegoRequestParser::class => 'mailer.webhook.request_parser.sweego',

src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer_webhook.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\Mailer\Bridge\Mailjet\Webhook\MailjetRequestParser;
2222
use Symfony\Component\Mailer\Bridge\Mailomat\RemoteEvent\MailomatPayloadConverter;
2323
use Symfony\Component\Mailer\Bridge\Mailomat\Webhook\MailomatRequestParser;
24+
use Symfony\Component\Mailer\Bridge\Mailtrap\RemoteEvent\MailtrapPayloadConverter;
25+
use Symfony\Component\Mailer\Bridge\Mailtrap\Webhook\MailtrapRequestParser;
2426
use Symfony\Component\Mailer\Bridge\Postmark\RemoteEvent\PostmarkPayloadConverter;
2527
use Symfony\Component\Mailer\Bridge\Postmark\Webhook\PostmarkRequestParser;
2628
use Symfony\Component\Mailer\Bridge\Resend\RemoteEvent\ResendPayloadConverter;
@@ -62,6 +64,11 @@
6264
->args([service('mailer.payload_converter.postmark')])
6365
->alias(PostmarkRequestParser::class, 'mailer.webhook.request_parser.postmark')
6466

67+
->set('mailer.payload_converter.mailtrap', MailtrapPayloadConverter::class)
68+
->set('mailer.webhook.request_parser.mailtrap', MailtrapRequestParser::class)
69+
->ar 10000 gs([service('mailer.payload_converter.mailtrap')])
70+
->alias(MailtrapRequestParser::class, 'mailer.webhook.request_parser.mailtrap')
71+
6572
->set('mailer.payload_converter.resend', ResendPayloadConverter::class)
6673
->set('mailer.webhook.request_parser.resend', ResendRequestParser::class)
6774
->args([service('mailer.payload_converter.resend')])
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Mailtrap\RemoteEvent;
13+
14+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
15+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
16+
use Symfony\Component\RemoteEvent\Exception\ParseException;
17+
use Symfony\Component\RemoteEvent\PayloadConverterInterface;
18+
use Symfony\Component\RemoteEvent\RemoteEvent;
19+
20+
/**
21+
* @author Kevin Bond <kevinbond@gmail.com>
22+
*/
23+
final class MailtrapPayloadConverter implements PayloadConverterInterface
24+
{
25+
public function convert(array $payload): RemoteEvent
26+
{
27+
$type = match ($payload['event']) {
28+
'delivery' => MailerDeliveryEvent::DELIVERED,
29+
'open' => MailerEngagementEvent::OPEN,
30+
'click' => MailerEngagementEvent::CLICK,
31+
'unsubscribe' => MailerEngagementEvent::UNSUBSCRIBE,
32+
'spam' => MailerEngagementEvent::SPAM,
33+
'soft bounce', 'bounce' => MailerDeliveryEvent::BOUNCE,
34+
'suspension', 'reject' => MailerDeliveryEvent::DROPPED,
35+
default => throw new ParseException(\sprintf('Unsupported event "%s".', $payload['event'])),
36+
};
37+
38+
if (\in_array($type, [MailerDeliveryEvent::DELIVERED, MailerDeliveryEvent::BOUNCE, MailerDeliveryEvent::DROPPED], true)) {
39+
$event = new MailerDeliveryEvent($type, $payload['message_id'], $payload);
40+
$event->setReason($payload['reason'] ?? $payload['response'] ?? '');
41+
} else {
42+
$event = new MailerEngagementEvent($type, $payload['message_id'], $payload);
43+
}
44+
45+
if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['timestamp'])) {
46+
throw new ParseException(\sprintf('Invalid date "%s".', $payload['timestamp']));
47+
}
48+
49+
$event->setDate($date);
50+
$event->setRecipientEmail($payload['email']);
51+
52+
if (isset($payload['category'])) {
53+
$event->setTags([$payload['category']]);
54+
}
55+
56+
if (isset($payload['custom_variables'])) {
57+
$event->setMetadata($payload['custom_variables']);
58+
}
59+
60+
return $event;
61+
}
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"events": [
3+
{
4+
"event": "bounce",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000001",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"response": "[CS01] Message rejected due to local policy",
15+
"response_code": 555,
16+
"bounce_category": "spam"
17+
},
18+
{
19+
"event": "click",
20+
"category": "Password reset",
21+
"custom_variables": {
22+
"variable_a": "value",
23+
"variable_b": "value2"
24+
},
25+
"message_id": "00000000-0000-0000-0000-000000000002",
26+
"email": "receiver@example.com",
27+
"timestamp": 1726358034,
28+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
29+
"ip": "142.86.27.2",
30+
"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",
31+
"url": "https://mailtrap.io/email-api"
32+
}
33+
]
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
5+
6+
$wh1 = new MailerDeliveryEvent(MailerDeliveryEvent::BOUNCE, '00000000-0000-0000-0000-000000000001', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
7+
$wh1->setRecipientEmail('receiver@example.com');
8+
$wh1->setTags(['Password reset']);
9+
$wh1->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
10+
$wh1->setReason('[CS01] Message rejected due to local policy');
11+
$wh1->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
12+
13+
$wh2 = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '00000000-0000-0000-0000-000000000002', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][1]);
14+
$wh2->setRecipientEmail('receiver@example.com');
15+
$wh2->setTags(['Password reset']);
16+
$wh2->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
17+
$wh2->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
18+
19+
return [$wh1, $wh2];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"events": [
3+
{
4+
"event": "bounce",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"response": "[CS01] Message rejected due to local policy",
15+
"response_code": 555,
16+
"bounce_category": "spam"
17+
}
18+
]
19+
}
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::BOUNCE, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setReason('[CS01] Message rejected due to local policy');
10+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
11+
12+
return [$wh];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"events": [
3+
{
4+
"event": "click",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"ip": "142.86.27.2",
15+
"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",
16+
"url": "https://mailtrap.io/email-api"
17+
}
18+
]
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
10+
11+
return [$wh];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"events": [
3+
{
4+
"event": "delivery",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc"
14+
}
15+
]
16+
}
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, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setReason('');
10+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
11+
12+
return [$wh];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"events": [
3+
{
4+
"event": "open",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11 10000 +
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"ip": "127.138.158.185",
15+
"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"
16+
}
17+
]
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::OPEN, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
10+
11+
return [$wh];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"events": [
3+
{
4+
"event": "reject",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"reason": "Recipient in suppression list. Reason: unsubscription"
15+
}
16+
]
17+
}
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::DROPPED, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setReason('Recipient in suppression list. Reason: unsubscription');
10+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
11+
12+
return [$wh];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"events": [
3+
{
4+
"event": "soft bounce",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"response": "4.7.1 Temporary error, please retry",
15+
"response_code": 451,
16+
"bounce_category": "greylisting"
17+
}
18+
]
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+< F438 /span>
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
4+
5+
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::BOUNCE, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setReason('4.7.1 Temporary error, please retry');
10+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
11+
12+
return [$wh];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"events": [
3+
{
4+
"event": "spam",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc"
14+
}
15+
]
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
4+
5+
$wh = new MailerEngagementEvent(MailerEngagementEvent::SPAM, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
10+
11+
return [$wh];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"events": [
3+
{
4+
"event": "suspension",
5+
"category": "Password reset",
6+
"custom_variables": {
7+
"variable_a": "value",
8+
"variable_b": "value2"
9+
},
10+
"message_id": "00000000-0000-0000-0000-000000000000",
11+
"email": "receiver@example.com",
12+
"timestamp": 1726358034,
13+
"event_id": "bede7236-2284-43d6-a953-1fdcafd0fdbc",
14+
"reason": "Your account has reached its daily sending limit."
15+
}
16+
]
17+
}
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::DROPPED, '00000000-0000-0000-0000-000000000000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)['events'][0]);
6+
$wh->setRecipientEmail('receiver@example.com');
7+
$wh->setTags(['Password reset']);
8+
$wh->setMetadata(['variable_a' => 'value', 'variable_b' => 'value2']);
9+
$wh->setReason('Your account has reached its daily sending limit.');
10+
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1726358034));
11+
12+
return [$wh];

0 commit comments

Comments
 (0)
0