Skip to content

Commit b17f374

Browse files
committed
[Mailer] enable Sendgrid to parse multiple events
1 parent 3e69cf6 commit b17f374

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

Tests/Webhook/Fixtures/batch.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{"email":"[email protected]","event":"dropped","reason":"Bounced Address","sg_event_id":"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA","sg_message_id":"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0","smtp-id":"<[email protected]>","timestamp":1600112492},
3+
{"email":"[email protected]","event":"click","sg_event_id":"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA","sg_message_id":"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0","smtp-id":"<[email protected]>","timestamp":1600112492}
4+
]

Tests/Webhook/Fixtures/batch.php

+19
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::DROPPED, 'LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)[0]);
7+
$wh1->setRecipientEmail('[email protected]');
8+
$wh1->setTags([]);
9+
$wh1->setMetadata([]);
10+
$wh1->setReason('Bounced Address');
11+
$wh1->setDate(\DateTimeImmutable::createFromFormat('U', 1600112492));
12+
13+
$wh2 = new MailerEngagementEvent(MailerEngagementEvent::CLICK, 'LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)[1]);
14+
$wh2->setRecipientEmail('[email protected]');
15+
$wh2->setTags([]);
16+
$wh2->setMetadata([]);
17+
$wh2->setDate(\DateTimeImmutable::createFromFormat('U', 1600112492));
18+
19+
return [$wh1, $wh2];

Tests/Webhook/Fixtures/webhook.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
$wh->setReason('Bounced Address');
1010
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1600112492));
1111

12-
return $wh;
12+
return [$wh];

Tests/Webhook/SendgridSignedRequestParserTest.php

+26-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@
2424
*/
2525
class SendgridSignedRequestParserTest extends AbstractRequestParserTestCase
2626
{
27+
private const PRIVATE_KEY = <<<'KEY'
28+
-----BEGIN RSA PRIVATE KEY-----
29+
MIICWgIBAAKBgHH/ZmiTGDi6/1IIx4vOKedN24Zuxj9G0ioNpCbNssQlukWijQiz
30+
UaOZ98JgEA11jGY1gFwCKYVSH5e1ZWN+m4hdxNQoNn8QaODzyo2ocGbobzrIuMJp
31+
mroyl6WmNa0jW8DMoW1Mpsxo/Vw9FrkAL+eSYgR8ZFWeXbcD8yRfVm/lAgMBAAEC
32+
gYBDqSUtWHD96u9zz0Yw0pLIeMudBM6h6/T9hM8zQM+j4AipIAu5aEVCZzZIph+g
33+
/W3xlDu1YIsoWE/sCXw+C31gLgDAd/4++G+3nuQumv5TgdWyZkXrFZ+HiPk77fqh
34+
F6U+5vTSYS/BOueisDUY7ndgf9pFVZtj5rKHHOmL26KFgQJBAK6npY3H1UyYHi/t
35+
vaxH/5KVqBDWuUE1+MjyVF0KbjyZOzMka7/4DenbBsZRDCqNrP8psuCwOFPf+vwN
36+
uVmE7vECQQCnF4F/INbeZkL3EQTMhCF3kIuY9jtB/ah+FQ/zom0gcw4zNAzKVeFm
37+
SmCTAeZbqq+fTFgwueIE4mPv4hiT0Hg1AkBIqoGr6p+pPYUZxd1rh40i7Nc/Ikdz
38+
hUQcPw6woz1YQxypW5blCQyo5rL74g6gyc9XXn8JEuhspTzkj8U1JKTRAkASdDAj
39+
IDda3KRssP58r+MaV2ZzgE5PHXqsYhse50NyIALjeM4o0o9QQsqjscQFP7ahu0bK
40+
Kt1heLdc2PWp7Y45AkAdAVZd//vS9FLU397DZAf7h+5qhUmPkm8vxnehCH+olQXq
41+
SPExlMI7PVpISz7jk9hRF31NStTZok//CUcq+yxJ
42+
-----END RSA PRIVATE KEY-----
43+
KEY;
44+
private const SECRET = 'MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHH/ZmiTGDi6/1IIx4vOKedN24Zuxj9G0ioNpCbNssQlukWijQizUaOZ98JgEA11jGY1gFwCKYVSH5e1ZWN+m4hdxNQoNn8QaODzyo2ocGbobzrIuMJpmroyl6WmNa0jW8DMoW1Mpsxo/Vw9FrkAL+eSYgR8ZFWeXbcD8yRfVm/lAgMBAAE=';
45+
2746
protected function createRequestParser(): RequestParserInterface
2847
{
2948
return new SendgridRequestParser(new SendgridPayloadConverter(), true);
@@ -34,15 +53,19 @@ protected function createRequestParser(): RequestParserInterface
3453
*/
3554
protected function createRequest(string $payload): Request
3655
{
56+
$payload = str_replace("\n", "\r\n", $payload);
57+
58+
openssl_sign('1600112502'.$payload, $signature, self::PRIVATE_KEY, \OPENSSL_ALGO_SHA256);
59+
3760
return Request::create('/', 'POST', [], [], [], [
3861
'Content-Type' => 'application/json',
39-
'HTTP_X-Twilio-Email-Event-Webhook-Signature' => 'MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=',
62+
'HTTP_X-Twilio-Email-Event-Webhook-Signature' => base64_encode($signature),
4063
'HTTP_X-Twilio-Email-Event-Webhook-Timestamp' => '1600112502',
41-
], str_replace("\n", "\r\n", $payload));
64+
], $payload);
4265
}
4366

4467
protected function getSecret(): string
4568
{
46-
return 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g==';
69+
return self::SECRET;
4770
}
4871
}

Webhook/SendgridRequestParser.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ protected function getRequestMatcher(): RequestMatcherInterface
4141
]);
4242
}
4343

44-
protected function doParse(Request $request, string $secret): ?AbstractMailerEvent
44+
/**
45+
* @return AbstractMailerEvent[]
46+
*/
47+
protected function doParse(Request $request, string $secret): array
4548
{
4649
$content = $request->toArray();
4750
if (
@@ -69,7 +72,7 @@ protected function doParse(Request $request, string $secret): ?AbstractMailerEve
6972
}
7073

7174
try {
72-
return $this->converter->convert($content[0]);
75+
return array_map($this->converter->convert(...), $content);
7376
} catch (ParseException $e) {
7477
throw new RejectWebhookException(406, $e->getMessage(), $e);
7578
}

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
},
2222
"require-dev": {
2323
"symfony/http-client": "^6.4|^7.0",
24-
"symfony/webhook": "^6.4|^7.0"
24+
"symfony/webhook": "^7.2"
2525
},
2626
"conflict": {
2727
"symfony/mime": "<6.4",
28-
"symfony/http-foundation": "<6.4"
28+
"symfony/http-foundation": "<6.4",
29+
"symfony/webhook": "<7.2"
2930
},
3031
"autoload": {
3132
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Sendgrid\\": "" },

0 commit comments

Comments
 (0)