Give sign-in codes a deadline

Give sign-in codes a deadline

Use deliverBy so a code that would arrive late is never sent.

On this page
  1. Set it
  2. What happens
  3. Tell the person
  4. Without an SDK
  5. Watch out

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

await mailset.send({
	from: 'Acme <moc.elpmaxe@olleh>',
	to: 'gro.elpmaxe@ana',
	subject: 'Your Acme code',
	text: 'Your code is 123456. It works for 10 minutes.',
	deliverBy: '10m',
});
text = "Your code is 123456. It works for 10 minutes."
mailset.send(
    {
        "from": "Acme <moc.elpmaxe@olleh>",
        "to": "gro.elpmaxe@ana",
        "subject": "Your Acme code",
        "text": text,
        "deliver_by": "10m",
    }
)
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 <moc.elpmaxe@olleh>",
	To:        []string{"gro.elpmaxe@ana"},
	Subject:   "Your Acme code",
	Text:      text,
	DeliverBy: mailset.Within(10 * time.Minute),
})
mailset.send_email(
  from: 'Acme <moc.elpmaxe@olleh>',
  to: 'gro.elpmaxe@ana',
  subject: 'Your Acme code',
  text: 'Your code is 123456. It works for 10 minutes.',
  deliver_by: '10m'
)
$text = 'Your code is 123456. It works for 10 minutes.';
$mailset->send([
    'from' => 'Acme <moc.elpmaxe@olleh>',
    'to' => 'gro.elpmaxe@ana',
    'subject' => 'Your Acme code',
    'text' => $text,
    'deliverBy' => '10m',
]);
mailset.send(Message.builder()
        .from("Acme <moc.elpmaxe@olleh>")
        .to("gro.elpmaxe@ana")
        .subject("Your Acme code")
        .text("Your code is 123456. "
                + "It works for 10 minutes.")
        .deliverBy("10m")
        .build());
let text =
    "Your code is 123456. It works for 10 minutes.";
let code = Message::builder()
    .from("Acme <moc.elpmaxe@olleh>")
    .to("gro.elpmaxe@ana")
    .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.
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);
}
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)
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
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
try {
    $mailset->send([...$code, 'deliverBy' => '10m']);
} catch (RateLimitException $error) {
    // limit_reached: it couldn't go in time.
    showTryAgainAt($error->retryAt);
}
try {
    mailset.send(code().deliverBy("10m").build());
} catch (RateLimitException error) {
    // limit_reached: it couldn't go in time.
    showTryAgainAt(error.getRetryAt());
}
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.

if (event.type === 'expired') {
	await offerNewCode(event.recipient);
}
if event["type"] == "expired":
    offer_new_code(event["recipient"])
import (
	"io"
	"log"
	"net/http"
	"os"

	mailset "mailset.app/go"
)

if event.Type == mailset.EventExpired {
	offerNewCode(event.Recipient)
}
if event.type == 'expired'
  Codes.offer_new(event.recipient)
end
if ($event instanceof ExpiredEvent) {
    offerNewCode($event->recipient);
}
if (event instanceof MailEvent.Expired expired) {
    offerNewCode(expired.recipient());
}
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.