Keep a key alive

Keep a key alive

Check your key at boot and daily, so it never stops for lack of use.

On this page
  1. Check at boot and daily
  2. What it tells you
  3. Change what a domain key sends as
  4. If it is about to stop
  5. Watch out

An app key that goes 90 days without a signed call stops working. That protects you from keys nobody remembers, and it can surprise a product that sends rarely. One call a day prevents it.

Check at boot and daily

import { Mailset } from 'mailset';

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

// When the server starts, and once a day.
const status = await mailset.key.check();

if (status.paused) console.warn('Paused:', status.paused);
console.log('Works until', new Date(status.expiresAt));
// A mailbox key sends as its mailbox.
for (const { address, sending } of status.mailboxes)
	if (sending !== 'ready') console.warn(address, sending);
// A domain key sends as the addresses it names.
for (const { name, sending, from } of status.domains) {
	const as = from?.join(', ') ?? 'any address';
	console.log(name, 'sends as', as);
	if (sending !== 'ready') console.warn(name, sending);
}
from datetime import datetime, timezone

from mailset import Mailset

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

# When the server starts, and once a day.
status = mailset.key.check()

if status["paused"]:
    print("Paused:", status["paused"])
ends = status["expires_at"] / 1000
until = datetime.fromtimestamp(ends, timezone.utc)
print("Works until", until)
# A mailbox key sends as its mailbox.
for box in status["mailboxes"]:
    if box["sending"] != "ready":
import (
	"context"
	"fmt"
	"log"
	"strings"
	"time"

	mailset "mailset.app/go"
)

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

mailset = Mailset::Client.new

# When the server starts, and once a day.
status = mailset.key.check

warn "Paused: #{status.paused}" if status.paused
puts "Works until #{Time.at(status.expires_at / 1000)}"
# A mailbox key sends as its mailbox.
status.mailboxes.each do |box|
  next if box.sending == 'ready'

  warn "#{box.address}: #{box.sending}"
end
# A domain key sends as the addresses it names.
status.domains.each do |domain|
  as = domain.from&.join(', ') || 'any address'
  puts "#{domain.name} sends as #{as}"
  next if domain.sending == 'ready'

  warn "#{domain.name}: #{domain.sending}"
end
<?php

require 'vendor/autoload.php';

use Mailset\Types\SendingState;

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

// When the server starts, and once a day.
$status = $mailset->key->check();

if ($status->paused !== null) {
    error_log("Paused: {$status->paused}");
}
$until = intdiv($status->expiresAt, 1000);
echo 'Works until ', gmdate('c', $until), PHP_EOL;
import app.mailset.Mailset;
import app.mailset.model.GrantDomain;
import app.mailset.model.GrantMailbox;
import app.mailset.model.KeyStatus;
import java.time.Instant;

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

// When the server starts, and once a day.
KeyStatus status = mailset.key().check();

status.paused().ifPresent(why ->
        System.err.println("Paused: " + why));
System.out.println("Works until "
        + Instant.ofEpochMilli(status.expiresAt()));
// A mailbox key sends as its mailbox.
for (GrantMailbox box : status.mailboxes())
use mailset::types::SendingState;
use mailset::Client;

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

// When the server starts, and once a day.
let status = mailset.key().check().await?;

if let Some(paused) = &status.paused {
    eprintln!("Paused: {paused}");
}
println!("Works until {} (ms)", status.expires_at);
// A mailbox key sends as its mailbox.
for b in &status.mailboxes {
    if b.sending != SendingState::Ready {
        eprintln!("{} {}", b.address, b.sending);
    }
mailset grants current

Without an SDK it is grants.current with an empty input, the call every client in Calling the API without an SDK makes.

What it tells you

FieldWhat it is
nameThe key's name, as approved
expiresAtWhen it stops, in milliseconds. Every signed call moves it 90 days out
pausedWhy Mailset stopped this key's email, or null
mailboxesA mailbox key's mailbox, the addresses it may send as, and whether its domain is ready. Empty for a domain key
domainsA domain key's domain, whether it is ready, and in from the addresses it sends as there (null: any address). Empty for a mailbox key

Run it when your server starts, so a revoked or mistyped key shows in your logs before a customer misses an email.

Change what a domain key sends as

mailset keys update <id> --from moc.elpmaxe@stpiecer --from moc.elpmaxe@ylperon replaces a domain key's list, and --any-address in its place lets it send as any address on its domain. Either asks for your passkey. Only whoever approved the key can change it, and only while they're an owner or admin of the domain's org. The key and its secret stay the same.

A send from an address the key doesn't name is refused with forbidden, and the message names the ones it may use: "You can't send as moc.elpmaxe@oec. This key sends as moc.elpmaxe@stpiecer and moc.elpmaxe@ylperon."

If it is about to stop

Mailset writes to whoever approved the key 14 days and 3 days before, with a link that keeps the same key. mailset keys renew <id> does the same from a terminal, and mailset keys list shows every key with its id.

A key already past its end is gone. Make a new one with mailset keys create, and put it in your secret store in the old one's place.

Watch out

  • One key per kind of email. Each key is paused on its own, so sign-in codes on one key and announcements on another never stop each other.
  • A key is a secret. It goes in your secret store, never in a repository or a browser.
  • mailset keys revoke <id> ends a key at once.