# Give sign-in codes a deadline

A sign-in code that arrives an hour late is worse than none: the person has given up, and the code may no longer work. `deliverBy` tells Mailset the latest a message may go.

## Set it

```typescript
await mailset.send({
	from: 'Acme <hello@example.com>',
	to: 'ana@example.org',
	subject: 'Your Acme code',
	text: 'Your code is 123456. It works for 10 minutes.',
	deliverBy: '10m',
});
```

```python
text = "Your code is 123456. It works for 10 minutes."
mailset.send(
    {
        "from": "Acme <hello@example.com>",
        "to": "ana@example.org",
        "subject": "Your Acme code",
        "text": text,
        "deliver_by": "10m",
    }
)
```

```go
import (
	"context"
	"log"
	"time"

	mailset "mailset.app/go"
)

text := "Your code is 123456. It works for 10 minutes."
_, err = client.Send(ctx, mailset.Message{
	From:      "Acme <hello@example.com>",
	To:        []string{"ana@example.org"},
	Subject:   "Your Acme code",
	Text:      text,
	DeliverBy: mailset.Within(10 * time.Minute),
})
```

```ruby
mailset.send_email(
  from: 'Acme <hello@example.com>',
  to: 'ana@example.org',
  subject: 'Your Acme code',
  text: 'Your code is 123456. It works for 10 minutes.',
  deliver_by: '10m'
)
```

```php
$text = 'Your code is 123456. It works for 10 minutes.';
$mailset->send([
    'from' => 'Acme <hello@example.com>',
    'to' => 'ana@example.org',
    'subject' => 'Your Acme code',
    'text' => $text,
    'deliverBy' => '10m',
]);
```

```java
mailset.send(Message.builder()
        .from("Acme <hello@example.com>")
        .to("ana@example.org")
        .subject("Your Acme code")
        .text("Your code is 123456. "
                + "It works for 10 minutes.")
        .deliverBy("10m")
        .build());
```

```rust
let text =
    "Your code is 123456. It works for 10 minutes.";
let code = Message::builder()
    .from("Acme <hello@example.com>")
    .to("ana@example.org")
    .subject("Your Acme code")
    .text(text)
    .deliver_by("10m");
mailset.send(code).await?;
```

The SDK takes a `Date`, or a time from now: `'30s'`, `'10m'`, `'2h'`.

## What happens

- **A limit would hold it past the deadline.** The send is refused at once with `limit_reached`, and `retryAt` says the soonest it could go. Nothing is queued.
- **It is queued and the deadline comes.** Mailset drops it unsent. The delivery log says `expired` and your webhook gets an `expired` event.
- **Otherwise** it goes as usual.

```typescript
try {
	await mailset.send({ ...code, deliverBy: '10m' });
} catch (error) {
	if (!(error instanceof MailsetError)) throw error;
	if (error.code !== 'limit_reached') throw error;
	return showTryAgainAt(error.retryAt);
}
```

```python
try:
    mailset.send({**code, "deliver_by": "10m"})
except RateLimitError as error:
    # limit_reached: it couldn't go before its deadline.
    show_try_again_at(error.retry_at)
```

```go
import (
	"context"
	"errors"
	"log"
	"time"

	mailset "mailset.app/go"
)

message := code
message.DeliverBy = mailset.Within(10 * time.Minute)
_, err := client.Send(ctx, message)
var refused *mailset.Error
if !errors.As(err, &refused) {
	return err
}
if refused.Code != mailset.CodeLimitReached {
	return err
}
showTryAgainAt(refused.RetryAt)
return nil
```

```ruby
begin
  mailset.send_email(**code, deliver_by: '10m')
rescue Mailset::RateLimitError => e
  # limit_reached: retry_at is the soonest it could go.
  show_try_again_at(e.retry_at)
end
```

```php
try {
    $mailset->send([...$code, 'deliverBy' => '10m']);
} catch (RateLimitException $error) {
    // limit_reached: it couldn't go in time.
    showTryAgainAt($error->retryAt);
}
```

```java
try {
    mailset.send(code().deliverBy("10m").build());
} catch (RateLimitException error) {
    // limit_reached: it couldn't go in time.
    showTryAgainAt(error.getRetryAt());
}
```

```rust
let message = code().deliver_by("10m");
if let Err(error) = mailset.send(message).await {
    if error.code() != "limit_reached" {
        return Err(error);
    }
    show_try_again_at(error.retry_at());
}
```

## Tell the person

On an `expired` event, the code never went. Offer a new one on the page they are waiting on.

```typescript
if (event.type === 'expired') {
	await offerNewCode(event.recipient);
}
```

```python
if event["type"] == "expired":
    offer_new_code(event["recipient"])
```

```go
import (
	"io"
	"log"
	"net/http"
	"os"

	mailset "mailset.app/go"
)

if event.Type == mailset.EventExpired {
	offerNewCode(event.Recipient)
}
```

```ruby
if event.type == 'expired'
  Codes.offer_new(event.recipient)
end
```

```php
if ($event instanceof ExpiredEvent) {
    offerNewCode($event->recipient);
}
```

```java
if (event instanceof MailEvent.Expired expired) {
    offerNewCode(expired.recipient());
}
```

```rust
if let Event::Expired(expired) = &event {
    offer_new_code(&expired.recipient).await;
}
```

## Without an SDK

`deliverBy` on `messages.submit`, and on each message of `messages.submitMany`, is a time in milliseconds since the epoch.

```http
POST /v1/messages.submit
Content-Type: application/json

{
	"input": {
		"data": "<base64 of the message>",
		"transactional": true,
		"deliverBy": 1800000600000
	}
}
```

## Watch out

- Match the deadline to the code's own life. A code good for 10 minutes gets `deliverBy: '10m'` or less.
- Use it on anything useless late: one-time links, "your driver is here". Leave it off receipts, which are fine an hour on.
- Keep sign-in codes on their own key, so a paused announcements key never stops them.

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content