Send email from Trigger.dev with Shipmail
Send email from a Trigger.dev task. The task retries when Shipmail is unavailable, and an idempotency key makes sure a retry does not send the email twice.
Last updated
Before you start
- A verified sending subdomain, such as
notify.example.com, with an active sender. See Transactional email. - An API key with send access to the sender's mailbox.
- The sender address, such as
hello@notify.example.com. Use the bare address. Shipmail adds the sender name.
Set the environment variables
Add them to each environment in the Trigger.dev dashboard. Tasks read them from process.env.
SHIPMAIL_API_KEY=sm_live_...
EMAIL_FROM=hello@notify.example.comAdd the task
A retry is a new attempt of the same run, so ctx.run.id does not change. Shipmail sees the same Idempotency-Key and sends once. Shipmail keeps a key for 24 hours. See Idempotency.
// trigger/send-email.ts
import { task } from "@trigger.dev/sdk";
type Email = { to: string; subject: string; text?: string; html?: string };
export const sendEmail = task({
id: "send-email",
retry: { maxAttempts: 3 },
run: async ({ to, ...content }: Email, { ctx }) => {
const res = await fetch("https://shipmail.to/api/v1/messages", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.SHIPMAIL_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": ctx.run.id,
},
body: JSON.stringify({ from: process.env.EMAIL_FROM, to: [to], ...content }),
});
if (!res.ok) throw new Error(`Shipmail returned ${res.status}: ${await res.text()}`);
},
});Trigger it
Trigger the task from your backend. The call returns once the run is queued.
import { sendEmail } from "@/trigger/send-email";
await sendEmail.trigger({
to: "customer@example.com",
subject: "Welcome to Acme",
text: "Thanks for signing up.",
});Test without sending
Use a sm_test_ key while you build. Shipmail accepts the request, delivers nothing and lists the send under Test in Activity. See Sandbox mode.