Retry without sending twice

Retry without sending twice

Make a send safe to repeat with an idempotency key or a Message-ID.

On this page
  1. With the SDK
  2. Without an SDK
  3. The rules
  4. When the same email went twice
  5. Which errors to retry
  6. Watch out

A request that times out may still have been taken. Send it again blindly and the person gets two receipts. Give each send a name of its own, and a retry sends nothing new.

With the SDK

import { Mailset, type SendInput } from 'mailset';

// Reads MAILSET_KEY from the environment.
const mailset = new Mailset();

const receipt: SendInput = {
	from: 'Acme <moc.elpmaxe@olleh>',
	to: 'gro.elpmaxe@ana',
	subject: 'Your receipt',
	text: 'Thanks for your order.',
	idempotencyKey: 'receipt-8842',
};

const first = await mailset.send(receipt);
// After a timeout or a crash, send it again.
// The same key answers the first receipt,
// and nothing goes out twice.
const again = await mailset.send(receipt);

console.log(first.id === again.id);
from mailset import Mailset, SendInput

# Reads MAILSET_KEY from the environment.
mailset = Mailset()

receipt: SendInput = {
    "from": "Acme <moc.elpmaxe@olleh>",
    "to": "gro.elpmaxe@ana",
    "subject": "Your receipt",
    "text": "Thanks for your order.",
    "idempotency_key": "receipt-8842",
}

first = mailset.send(receipt)
# After a timeout or a crash, send it again.
# The same key answers the first receipt,
# and nothing goes out twice.
again = mailset.send(receipt)

print(first["id"] == again["id"])
import (
	"context"
	"fmt"
	"log"

	mailset "mailset.app/go"
)

client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
ctx := context.Background()

receipt := mailset.Message{
	From:           "Acme <moc.elpmaxe@olleh>",
	To:             []string{"gro.elpmaxe@ana"},
	Subject:        "Your receipt",
	Text:           "Thanks for your order.",
	IdempotencyKey: "receipt-8842",
}
require 'mailset'

mailset = Mailset::Client.new

receipt = {
  from: 'Acme <moc.elpmaxe@olleh>',
  to: 'gro.elpmaxe@ana',
  subject: 'Your receipt',
  text: 'Thanks for your order.',
  idempotency_key: 'receipt-8842'
}

first = mailset.send_email(**receipt)
# After a timeout or a crash, send it again.
# The same key answers the first receipt,
# and nothing goes out twice.
again = mailset.send_email(**receipt)

puts first.id == again.id
<?php

require 'vendor/autoload.php';

// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();

$receipt = [
    'from' => 'Acme <moc.elpmaxe@olleh>',
    'to' => 'gro.elpmaxe@ana',
    'subject' => 'Your receipt',
    'text' => 'Thanks for your order.',
    'idempotencyKey' => 'receipt-8842',
];

$first = $mailset->send($receipt);
// After a timeout or a crash, send it again.
// The same key answers the first receipt,
// and nothing goes out twice.
$again = $mailset->send($receipt);

var_dump($first->id === $again->id);
import app.mailset.Mailset;
import app.mailset.Message;
import app.mailset.Sent;

// Reads MAILSET_KEY from the environment.
Mailset mailset = Mailset.fromEnv();

Message receipt = Message.builder()
        .from("Acme <moc.elpmaxe@olleh>")
        .to("gro.elpmaxe@ana")
        .subject("Your receipt")
        .text("Thanks for your order.")
        .idempotencyKey("receipt-8842")
        .build();

Sent first = mailset.send(receipt);
// After a timeout or a crash, send it again.
// The same key answers the first receipt,
// and nothing goes out twice.
Sent again = mailset.send(receipt);

System.out.println(first.id().equals(again.id()));
use mailset::{Client, Message};

// Reads MAILSET_KEY from the environment.
let mailset = Client::from_env()?;

let receipt = Message::builder()
    .from("Acme <moc.elpmaxe@olleh>")
    .to("gro.elpmaxe@ana")
    .subject("Your receipt")
    .text("Thanks for your order.")
    .idempotency_key("receipt-8842")
    .build();

let first = mailset.send(&receipt).await?;
// After a timeout or a crash, send it again.
// The same key answers the first receipt,
// and nothing goes out twice.
let again = mailset.send(&receipt).await?;

println!("{}", first.id == again.id);

Call send again with the same key and you get the first send's receipt back, the same id and messageId. In an array, each message has its own key.

Without an SDK

The API has no separate key: the message's own Message-ID header is the name. Submit a Message-ID a mailbox (or a domain key's domain) already sent, and Mailset answers the first receipt and sends nothing.

  1. Make the Message-ID from something stable, such as a hash of receipt-8842, at your domain: <5f2a9c…@example.com>.
  2. Put it in the message you build.
  3. On a timeout or a 503, submit the same message again.

The SDK's idempotencyKey does exactly this for you.

The rules

  • A key belongs to its from address. Two addresses can both use welcome-1.
  • It has no time limit.
  • Only the key is compared, never the content. The same key with other words sends nothing: the first one wins.
  • A send that never went doesn't hold its key. If it was refused, or later failed or expired, the same key sends it again.
  • A key is 1 to 255 characters, with no line breaks.
  • Inside one batch a key is used once. The same key on two messages from the same address is refused whole, with invalid_input, before anything is sent: send([...]) throws and names the key and the items, counted from 0. Over HTTP, messages.submitMany refuses two messages with the same Message-ID from one mailbox or one domain key's domain the same way, and issues points at each one (["messages", 3]). In separate calls, the second still gets the first receipt.

When the same email went twice

Mailset can't stop a repeat it can't recognize. A send with no key, or with a new key each time, has a new name, so both go. Mailset still notices and says so:

  • In the delivery log, the second line has a "Duplicate?" mark in the app, and duplicateOf in mailset delivery search.
  • On your webhook, every event about the second message carries duplicateOf.

duplicateOf is the id of the first send. It means the same From address sent the same subject, text, HTML and files to the same recipient less than 10 minutes before, under another Message-ID. Both were sent, and nothing was held or refused.

The fix is a stable name: an idempotencyKey in the SDK, or the same Message-ID without one. Then the second send answers the first receipt and never goes.

Which errors to retry

Retry unavailable and internal, and a request that got no answer, waiting twice as long each time. Don't retry invalid_input or forbidden: the same request gets the same answer. Errors and limits has the whole table.

Watch out

  • Name the send after the thing it is about (receipt-<order id>, code-<attempt id>), never after the time.
  • A new sign-in code is a new send. Give each attempt its own key, or the person gets the old code's receipt and no email.