Skip to content

Email

Layeron Email gives your backend one place to send product email, receive replies, render React Email templates, and trigger follow-up work from inbound messages.

email.send(options) configures Cloudflare Email Sending for a domain. Outbound sends are accepted into a Job run so delivery has retry and failure state. email.receive(options, handler) configures Cloudflare Email Routing and sends the domain catch-all to the Email product Worker.

Terminal window
import { backend } from "@layeron/core"
import { email } from "@layeron/modules"
const app = backend()
const outbound = email.send({
name: "transactional",
domain: "mail.example.com",
email: "hello",
queue: {
consumer: {
concurrency: 4,
},
},
})
const welcome = outbound.template<{ user: { name: string } }>("welcome", {
subject: ({ user }) => `Welcome ${user.name}`,
text: ({ user }) => `Hello ${user.name}`,
})
const inbound = email.receive({
name: "support",
domain: "support.example.com",
}, async (message) => {
console.log(message.from, message.to, message.subject)
})
app.use(outbound)
app.use(inbound)
app.post("/welcome", async () => {
return await outbound.send({
to: "ada@example.com",
template: welcome,
payload: {
user: {
name: "Ada",
},
},
})
})

In local dev, outbound email is saved to .layeron/local/<environment>/email/outbox/. Test inbound email with /cgi-bin/receive-email/<address>.