From 8af3d02ef698540bf678e96abea2a83be0b2987d Mon Sep 17 00:00:00 2001 From: Alexander Hofbauer Date: Wed, 7 Feb 2024 10:57:24 +0100 Subject: [PATCH] [Mailer][Postmark][Webhook] Accept different date formats Postmark webhooks sometimes use "plain" ISO 6801 format, sometimes including 7 digits microseconds. As the PHP parameter only allows for 6 digits neither would parse without fallbacks. Fixes #53788 --- .../Postmark/RemoteEvent/PostmarkPayloadConverter.php | 9 ++++++++- .../Bridge/Postmark/Tests/Webhook/Fixtures/bounce.json | 2 +- .../Bridge/Postmark/Tests/Webhook/Fixtures/bounce.php | 2 +- .../Bridge/Postmark/Tests/Webhook/Fixtures/delivery.php | 2 +- .../Postmark/Tests/Webhook/Fixtures/link_click.php | 2 +- .../Bridge/Postmark/Tests/Webhook/Fixtures/open.php | 2 +- .../Postmark/Tests/Webhook/Fixtures/spam_complaint.json | 2 +- .../Postmark/Tests/Webhook/Fixtures/spam_complaint.php | 3 +-- .../Tests/Webhook/Fixtures/subscription_change.php | 2 +- 9 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/RemoteEvent/PostmarkPayloadConverter.php b/src/Symfony/Component/Mailer/Bridge/Postmark/RemoteEvent/PostmarkPayloadConverter.php index 9fd8cea6ac58e..3b3aabe55c0a1 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/RemoteEvent/PostmarkPayloadConverter.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/RemoteEvent/PostmarkPayloadConverter.php @@ -47,7 +47,14 @@ public function convert(array $payload): AbstractMailerEvent 'SpamComplaint' => $payload['BouncedAt'], default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['RecordType'])), }; - if (!$date = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', $payloadDate)) { + + $date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $payloadDate) + // microseconds, 6 digits + ?: \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', $payloadDate) + // microseconds, 7 digits + ?: \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u?P', $payloadDate); + + if (!$date) { throw new ParseException(sprintf('Invalid date "%s".', $payloadDate)); } $event->setDate($date); diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.json b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.json index d8658cbae3716..32cf5480ba17e 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.json +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.json @@ -16,7 +16,7 @@ "Details": "Test bounce details", "Email": "john@example.com", "From": "sender@example.com", - "BouncedAt": "2022-09-02T14:29:19Z", + "BouncedAt": "2022-09-02T14:29:19.1234567Z", "DumpAvailable": true, "Inactive": true, "CanActivate": true, diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.php index 5d2f453ce21ac..35a0a516880d8 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/bounce.php @@ -7,6 +7,6 @@ $wh->setTags(['Test']); $wh->setMetadata(['example' => 'value', 'example_2' => 'value']); $wh->setReason('The server was unable to deliver your message (ex: unknown user, mailbox not found).'); -$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', '2022-09-02T14:29:19Z')); +$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', '2022-09-02T14:29:19.123456Z')); return $wh; diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/delivery.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/delivery.php index 6adb84cbb2832..a05e371549815 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/delivery.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/delivery.php @@ -7,6 +7,6 @@ $wh->setTags(['welcome-email']); $wh->setMetadata(['example' => 'value', 'example_2' => 'value']); $wh->setReason(''); -$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', '2022-09-02T11:49:27Z')); +$wh->setDate(\DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, '2022-09-02T11:49:27Z')); return $wh; diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/link_click.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/link_click.php index dab89a07eed8b..ad656c208e2c9 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/link_click.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/link_click.php @@ -6,6 +6,6 @@ $wh->setRecipientEmail('john@example.com'); $wh->setTags(['welcome-email']); $wh->setMetadata(['example' => 'value', 'example_2' => 'value']); -$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', '2022-09-02T14:31:09Z')); +$wh->setDate(\DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, '2022-09-02T14:31:09Z')); return $wh; diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/open.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/open.php index cd91e6dcdafbb..d68184c6af8ff 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/open.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/open.php @@ -6,6 +6,6 @@ $wh->setRecipientEmail('john@example.com'); $wh->setTags(['welcome-email']); $wh->setMetadata(['example' => 'value', 'example_2' => 'value']); -$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', '2022-09-02T14:30:47Z')); +$wh->setDate(\DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, '2022-09-02T14:30:47Z')); return $wh; diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.json b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.json index 222819433d4fe..1ef818c69a8f4 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.json +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.json @@ -16,7 +16,7 @@ "Details": "Test spam complaint details", "Email": "john@example.com", "From": "sender@example.com", - "BouncedAt": "2022-09-02T14:29:57Z", + "BouncedAt": "2022-09-02T14:29:57.123456Z", "DumpAvailable": true, "Inactive": true, "CanActivate": false, diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.php index 87a22219c5bbb..7231a48d87884 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/spam_complaint.php @@ -6,6 +6,5 @@ $wh->setRecipientEmail('john@example.com'); $wh->setTags(['Test']); $wh->setMetadata(['example' => 'value', 'example_2' => 'value']); -$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', '2022-09-02T14:29:57Z')); - +$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', '2022-09-02T14:29:57.123456Z')); return $wh; diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/subscription_change.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/subscription_change.php index 65c82c972f8f6..0d7782493ddd5 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/subscription_change.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Webhook/Fixtures/subscription_change.php @@ -6,6 +6,6 @@ $wh->setRecipientEmail('john@example.com'); $wh->setTags(['welcome-email']); $wh->setMetadata(['example' => 'value', 'example_2' => 'value']); -$wh->setDate(\DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sT', '2022-09-02T14:32:30Z')); +$wh->setDate(\DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, '2022-09-02T14:32:30Z')); return $wh;