MailSlurp logo

guides

Send Email with the MailSlurp JavaScript SDK

Use MailSlurp SendEmailOptions in JavaScript and TypeScript. Learn the return type, HTML fields, attachment IDs, templates and receive-side checks.

Use with your tools

Use the MailSlurp JavaScript SDK when your backend or test suite needs to send from a programmable inbox. Pass the sender's inbox ID and a SendEmailOptions object; the SDK returns a sent-message record that you can associate with the operation.

Still choosing how to send? Sending email with JavaScript explains the browser, SMTP and backend options. For queues, scheduling and recipient controls, see the sending guide.

Core API call

In mailslurp-client 17.4.0, sendEmail(inboxId, sendEmailOptions) returns Promise<SentEmailDto>. It calls the send-and-confirm operation and resolves to the parsed DTO, not a Fetch Response: use fields such as sent.id rather than calling .json().

The first argument is the sender inbox's ID, not its email address. A returned sent record is not proof that the recipient received the message, that it rendered correctly or that an account action completed.

Add a typed send helper to your backend

Use Node.js 22 or later and install the SDK:

npm install mailslurp-client@17.4.0

Read MAILSLURP_API_KEY from your server environment, reject startup if it is missing, and create the client with new MailSlurp({ apiKey }). Keep the key out of browser bundles. Supply an inbox ID you control and the intended recipient address.

Save this helper as send-message.ts in your TypeScript project. It accepts the configured client and sends a small fictional welcome message:

import { MailSlurp, SendEmailOptions, SentEmailDto } from 'mailslurp-client';

export async function sendWelcome(
  mailslurp: MailSlurp,
  senderInboxId: string,
  recipientAddress: string,
): Promise<SentEmailDto> {
  const options: SendEmailOptions = {
    to: [recipientAddress],
    subject: 'Welcome, Ari',
    body: '<p>Hello Ari, your account is ready.</p>',
    isHTML: true,
  };
  return mailslurp.sendEmail(senderInboxId, options);
}

Call sendWelcome from your server or test code with those three arguments and await the returned record. This is a helper, not a standalone application or a browser send endpoint. Add a receive-side check when you need to verify delivery.

Common SendEmailOptions fields

Use the SDK's exported type rather than maintaining a copied interface. The fields used most often are:

  • to, cc and bcc: recipient address arrays.
  • subject: the message subject.
  • body and isHTML: a single body and whether to send it as HTML.
  • replyTo: the address that should receive replies.
  • attachments: IDs returned by an attachment upload, not local file paths or file bytes.
  • template and templateVariables: a stored template ID and its substitution data.

By default, the source inbox supplies the sender address. Use an authorized sender identity and the documented custom-domain setup when changing from. Refer to the JavaScript SDK documentation and send-and-confirm API operation for the complete options and response.

HTML and template variables

Use isHTML: true for markup in body. The older html option is deprecated and still supported in this SDK version; prefer isHTML for new code. Setting the flag does not itself supply a separately authored plain-text alternative. For explicit text and HTML parts, use the complete HTML creation and sending example.

Inline substitution uses placeholders in the supplied subject or body with templateVariables. A stored template is a separate resource selected through template; omit body when using it, and provide the variables that template expects. The template variables guide covers creating and using stored templates.

Check the installed SDK's templateVariables type before adapting an example: version 17.4.0 declares object-valued entries. Do not silence a type mismatch with a cast and assume that the payload matches your template. The helper above uses static content so its send contract can be used without a separate template setup.

Attachments in Node.js

Upload each file first, then pass the returned attachment IDs in SendEmailOptions.attachments. Reading a file and converting it to Base64 only prepares an upload payload; it does not attach the file to a message.

Use the Base64 upload guide for encoding and the attachment API guide for upload, send and download operations. After sending, check the received attachment name, content type and bytes when those are part of your application's contract.

Verify receipt separately

For an automated test, use an isolated recipient inbox, capture the time before triggering the send, then wait for the matching message with a bounded timeout. Assert the required subject, body, links or attachments. If the message supports signup or a password reset, complete that action and check the intended account state as well.

The receiving reference covers API retrieval, while the Playwright email testing guide shows browser continuation. Retain message IDs and bounded failure categories for diagnosis; keep message bodies, OTPs and tokenized links out of shared CI logs.