Skip to content

Commit

Permalink
bug #53829 [Mailer][Postmark][Webhook] Accept different date formats …
Browse files Browse the repository at this point in the history
…(aleho)

This PR was merged into the 6.4 branch.

Discussion
----------

[Mailer][Postmark][Webhook] Accept different date formats

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53788
| License       | MIT

Postmark webhooks sometimes use "plain" ISO 6801 format, sometimes including 7 digits microseconds.
We're currently seeing `2024-02-07T09:41:16.7048881Z` for example.

As the PHP parameter only allows for 6 digits neither would parse without fallbacks.

Commits
-------

8af3d02 [Mailer][Postmark][Webhook] Accept different date formats
  • Loading branch information
nicolas-grekas committed Feb 9, 2024
2 parents b449929 + 8af3d02 commit adbe494
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 10 deletions.
Expand Up @@ -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);
Expand Down
Expand Up @@ -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,
Expand Down
Expand Up @@ -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;
Expand Up @@ -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;
Expand Up @@ -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;
Expand Up @@ -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;
Expand Up @@ -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,
Expand Down
Expand Up @@ -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;
Expand Up @@ -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;

0 comments on commit adbe494

Please sign in to comment.