Skip to content

[Mailer][SendGrid] add support for scheduling delivery via send_at API parameter #60372

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Sendgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

7.3
---

* Add support for scheduling delivery with the `send_at` API parameter via a
`Send-At` date-header

7.2
---

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Sendgrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ class SendGridConsumer implements ConsumerInterface
}
```

Scheduling
----------

When using the **API transport** (with a `sendgrid+api` DSN), you can schedule
your emails by providing a `\DateTimeInterface` object in a
`Symfony\Component\Mime\Header\DateHeader` named `Send-At`.

```php
$email = new \Symfony\Component\Mime\Email();
$email->getHeaders()->addDateHeader('Send-At', (new \DateTime())->modify('+3 hours'));
```
It will be mapped to the `send_at` parameter of the `[POST] /mail/send`
[API endpoint](https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send#request-body)

Resources
---------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,18 @@ public function testInlineWithoutCustomContentId()

$this->assertSame('text.txt', $payload['attachments'][0]['content_id']);
}

public function testSendAtHeader()
{
$email = new Email();
$email->getHeaders()->addDateHeader('Send-At', new \DateTime('2025-05-07 16:00:00', new \DateTimeZone('Europe/Paris')));
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);

$transport = new SendgridApiTransport('ACCESS_KEY');
$method = new \ReflectionMethod(SendgridApiTransport::class, 'getPayload');
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('send_at', $payload);
$this->assertSame(1746626400, $payload['send_at']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\DateHeader;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -126,7 +127,12 @@ private function getPayload(Email $email, Envelope $envelope): array
continue;
}

if ($header instanceof TagHeader) {
if ('send-at' === $name) {
if (!$header instanceof DateHeader) {
throw new TransportException(sprintf('The Send-At header should be a "%s" instance to deduce the correct timestamp.', DateHeader::class));
}
$payload['send_at'] = $header->getDateTime()->getTimestamp();
} elseif ($header instanceof TagHeader) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,thank you for your suggestion.
I didn't want to change too much of the code to facilitate the review process but I'll gladly update the successive conditions if necessary. Is it something you would like me to do in this PR ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a suggestion, but since you've added new conditions, this approach might make sense, as it's used in other integrations. I think it's worth waiting to hear and consider other opinions before making a final decision.

if (10 === \count($categories)) {
throw new TransportException(\sprintf('Too many "%s" instances present in the email headers. Sendgrid does not accept more than 10 categories on an email.', TagHeader::class));
}
Expand Down