MailSlurp logo

guides

Send Email in JavaScript and Node.js with MailSlurp

Developer-focused guide for sending email in JavaScript and Node.js with MailSlurp. Covers SendEmailOptions, HTML, attachments, templates, and delivery-safe patterns.

View MarkdownAgent setup

This page is the JavaScript-focused companion to the broader sending emails guide. Use it when you already know you want the MailSlurp SDK or API send path in app code and test suites.

If you are still choosing between browser mailto:, Node.js SMTP, and backend email API patterns, start with Send email with JavaScript.

Core API call

The primary call shape is:

sendEmail(inboxId: string, sendEmailOptions: SendEmailOptions): Promise<Response>
  • inboxId: the MailSlurp inbox that sends the message.
  • sendEmailOptions: recipients, content, headers, and delivery options.

Minimum payload

to is the key field most workflows start with:

await mailslurp.sendEmail(inboxId, {
  to: ["user@mycompany.com"],
  subject: "Welcome",
  body: "Your account is ready",
});

Common SendEmailOptions fields

interface SendEmailOptions {
  to?: string[];
  cc?: string[];
  bcc?: string[];
  subject?: string;
  body?: string;
  html?: boolean;
  attachments?: string[];
  templateVariables?: Record<string, unknown>;
  replyTo?: string;
  from?: string;
  charset?: string;
}

Use named senders where needed:

  • Plain: qa@example.com
  • Named: "QA Team" <qa@example.com>

HTML and template variables

Set html: true when sending markup-based email:

await mailslurp.sendEmail(inboxId, {
  to: ["user@test.com"],
  subject: "Verify your account",
  body: "<p>Use code <strong>483921</strong></p>",
  html: true,
});

For reusable transactional messages, use template variables:

await mailslurp.sendEmail(inboxId, {
  to: ["user@test.com"],
  body: "Hello {{name}}",
  templateVariables: { name: "Ari" },
});

Attachments in Node.js

Workflow:

  1. Read file bytes.
  2. Convert to base64.
  3. Upload attachment.
  4. Send using returned attachment IDs.
import { readFileSync } from "fs";

const bytes = readFileSync("./invoice.pdf");
const base64Contents = Buffer.from(bytes).toString("base64");

See base64 uploads for language-specific patterns.

Production-safe sending checklist

  • Validate recipient quality before high-volume sends.
  • Use queue-backed send flows for retry resilience.
  • Add delivery/bounce webhooks for visibility.
  • Keep sender identity (SPF/DKIM/DMARC) aligned with your domain policy.

Related:

Next step

If this is part of test automation, move to receive emails in code and add deterministic receive-side assertions.