Deno and Bun both have fetch built in, so the quickest way to send email from either runtime is one POST to an email API, with nothing to install. If you need SMTP, both can run Nodemailer. The code is almost identical across the two runtimes; the differences are in permissions, environment variables and where you host.
Option 1: fetch and an email API
This function works unchanged in Deno, Bun and Node. It sends through Mailcandor; other email APIs take a similar JSON body.
// email.ts
export async function sendEmail(
apiKey: string,
email: Record<string, unknown>,
idempotencyKey?: string,
) {
const res = await fetch("https://api.mailcandor.com/v1/emails", {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
...(idempotencyKey ? { "Idempotency-Key": idempotencyKey } : {}),
},
body: JSON.stringify(email),
});
const data = await res.json();
if (!res.ok) throw new Error(`${data.error.type}: ${data.error.message}`);
return data as { id: string; status: string };
}
The response is a 202 with an id and "status": "queued". Delivery and bounces are reported later by webhook against that id. The optional Idempotency-Key makes retries safe: repeat the key and you get the first response back instead of a second email.
In Deno
Deno reads environment variables with Deno.env.get. Deno.serve starts an HTTP server:
// main.ts
import { sendEmail } from "./email.ts";
const apiKey = Deno.env.get("MAILCANDOR_KEY");
if (!apiKey) throw new Error("MAILCANDOR_KEY is not set");
Deno.serve(async (req) => {
if (req.method !== "POST") return new Response("Not found", { status: 404 });
const { email } = await req.json();
const { id } = await sendEmail(apiKey, {
from: "Acme <hello@acme.com>",
to: email,
subject: "Welcome to Acme",
html: "<p>Thanks for signing up.</p>",
}, `welcome-${email}`);
return Response.json({ id });
});
Run it with only the permissions it needs:
deno run --allow-net --allow-env=MAILCANDOR_KEY main.ts
--allow-net without a list lets the server listen and make requests. To lock outbound requests to the email API, list the hosts: --allow-net=api.mailcandor.com,0.0.0.0:8000. Deno also loads a .env file when you pass --env-file.
In Bun
Bun exposes environment variables on both process.env and Bun.env, and loads .env files automatically. Bun.serve starts a server:
// server.ts
import { sendEmail } from "./email.ts";
const apiKey = Bun.env.MAILCANDOR_KEY!;
Bun.serve({
port: 3000,
routes: {
"/signup": {
POST: async (req) => {
const { email } = await req.json();
const { id } = await sendEmail(apiKey, {
from: "Acme <hello@acme.com>",
to: email,
subject: "Welcome to Acme",
html: "<p>Thanks for signing up.</p>",
}, `welcome-${email}`);
return Response.json({ id });
},
},
},
});
Run it with bun run server.ts. No permission flags are needed; Bun, like Node, does not sandbox network access.
Attachments
Attachments travel in the JSON body as base64, in an attachments array of { filename, content, content_type } objects. Both runtimes read files easily:
// Deno
import { encodeBase64 } from "jsr:@std/encoding/base64";
const content = encodeBase64(await Deno.readFile("invoice-1042.pdf"));
// Bun
const content = Buffer.from(await Bun.file("invoice-1042.pdf").arrayBuffer()).toString("base64");
Then add attachments: [{ filename: "invoice-1042.pdf", content, content_type: "application/pdf" }] to the email. In Deno, reading the file also needs --allow-read.
Option 2: Nodemailer over SMTP
Use SMTP when you already have Nodemailer code to reuse or you must send through a specific relay.
In Bun, install it as usual:
bun add nodemailer
In Deno 2, import it straight from npm with no install step:
import nodemailer from "npm:nodemailer";
const transport = nodemailer.createTransport({
host: "smtp.mailcandor.com",
port: 465,
secure: true,
auth: { user: "mailcandor", pass: Deno.env.get("MAILCANDOR_KEY") },
});
await transport.sendMail({
from: "Acme <hello@acme.com>",
to: "sam@example.com",
subject: "Your login code",
html: "<p>Your code is <b>481 203</b></p>",
});
In Bun, change the import to import nodemailer from "nodemailer" and read the key from Bun.env. Use port 465 with secure: true, or port 587 with secure: false so Nodemailer upgrades with STARTTLS. The Node.js email guide covers more Nodemailer options.
Hosting: where SMTP may not work
Code that sends over SMTP on your laptop can fail once deployed, because many hosts restrict outbound mail ports to stop spam:
| Where it runs | Email API (fetch) | SMTP |
|---|---|---|
| Your own server or VM, Deno or Bun | Works | Works if the provider allows 465/587 |
| Deno Deploy | Works | Outbound SMTP ports have been restricted; check Deno’s current docs |
| Supabase Edge Functions (Deno) | Works | May be restricted; use the API |
| Containers on most clouds | Works | Port 25 often blocked, 465/587 usually open |
An HTTPS request to an email API goes out on port 443, which every host allows. For code that may move between hosts, that is the safer default. If you use Supabase, its Auth emails are configured separately; see our Supabase custom SMTP guide.
Send in the background
A web request should not wait for email. In both runtimes you can start the send without awaiting it and catch errors so they are logged rather than lost: sendEmail(...).catch(console.error). For anything that must not be dropped, such as receipts, write a job to a queue or database table first and send from a worker that retries with the same idempotency key.
Before you send: verify your domain
The runtime does not change the rules for inbox placement. Mail from a domain without SPF, DKIM and DMARC records is likely to be filtered as spam. Add your domain in your provider’s dashboard and publish its records; the SPF, DKIM and DMARC setup guide explains how.
Sending with Mailcandor
Mailcandor’s email API works from Deno and Bun with the fetch code above, and there is no SDK to import. The SMTP relay at smtp.mailcandor.com accepts the Nodemailer settings shown, with your API key as the password. Access is by request through the pricing page.