Send email from Inngest with Shipmail
Send email from an Inngest function in a step.run(). Inngest retries a step that throws, 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 where your Inngest functions run, such as your app's host.
SHIPMAIL_API_KEY=sm_live_...
EMAIL_FROM=hello@notify.example.comAdd the function
The example uses the v4 SDK. runId stays the same when Inngest retries the step, so Shipmail sees the same Idempotency-Key and sends once. Shipmail keeps a key for 24 hours. See Idempotency.
// src/inngest/functions.ts
import { inngest } from "./client";
export const sendWelcomeEmail = inngest.createFunction(
{ id: "send-welcome-email", triggers: { event: "app/user.signed_up" } },
async ({ event, step, runId }) => {
await step.run("send-email", async () => {
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": `welcome-${runId}`,
},
body: JSON.stringify({
from: process.env.EMAIL_FROM,
to: [event.data.email],
subject: "Welcome to Acme",
text: "Thanks for signing up.",
}),
});
if (!res.ok) throw new Error(`Shipmail returned ${res.status}: ${await res.text()}`);
});
},
);Serve and trigger it
Register the function with serve(), then send the event from your app.
// app/api/inngest/route.ts
import { serve } from "inngest/next";
import { inngest } from "@/inngest/client";
import { sendWelcomeEmail } from "@/inngest/functions";
export const { GET, POST, PUT } = serve({
client: inngest,
functions: [sendWelcomeEmail],
});await inngest.send({
name: "app/user.signed_up",
data: { email: "customer@example.com" },
});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.