# Sending emails

You can send emails from any MailSlurp inbox that you have created.

# Basic

The simplest email you can send only requires a recipient and an inbox.

// minimum example
await mailslurp.sendEmal(inbox.id, { to: ["me@test.com"] });

This will send an empty email from the inboxes email address to the recipient. More emails however are more complicated.

# Options

# Recipients

You can provide to, cc, and bcc recipient lists. Each must be in array format.

await mailslurp.sendEmal(inbox.id, {
  to: ["ceo@test.com"],
  cc: ["accounts@test.com", "staff@test.com"],
  bcc: ["secret@test.com"]
});

# Subject line

You can specify subject as follows:

await mailslurp.sendEmal(inbox.id, {
  to: ["me@test.com"],
  subject: "my subject line"
});

# Body

The body of an email is a string.

await mailslurp.sendEmal(inbox.id, {
  to: ["me@test.com"],
  body: "Hi there"
});

# HTML

You can also send HTML by setting html: true. All emails are UTF-8 by default and support i18n.

await mailslurp.sendEmal(inbox.id, {
  to: ["me@test.com"],
  body: `
  <article>
    <h1>γειά σου κόσμος</h1>
  </article>
`,
  html: true
});

TIP

When sending HTML you should use tables and inlined CSS for better rendering in most clients.

# Attachments

You can submit a list of attachment IDs when sending emails to attach files. You must upload the files before sending using the attachment upload endpoints.

// first upload attachments as base64 encoded string
const file = {
    base64Contents: "iVBORw0KGgoAAAANSUhE..."
    contentType: "image/png",
    filename: "pixel.png"
}
const attachmentId = await mailslurp.uploadAttachment(file)
// send email with attachment ID in array
await mailSlurp.sendEmail(inbox.id, {
  to: ['test@test.com'],
  attachments: [attachmentId]
})

# Encoding

MailSlurp expects files as base 64 encoded strings. You can do this by reading a file as bytes and then converting those to base64. Here is an example using NodeJS:

import { readFileSync } from "fs";
const bytes = readFileSync(path);
const base64Contents = new Buffer(bytes).toString("base64");

# Templates

MailSlurp supports sending emails with predefined templates.

# Capabilities

Templates can be created in code or designed in the dashboard. Templates support HTML, variables, and handlebars templating syntax.

TIP

Unlike other services MailSlurp will warn you if you try to send a template with missing variables.

# Creating templates

await mailslurp.createTemplate({ content: "Welcome to our app" });

# Variable syntax

If you want to use variables in your templates simple wrap each variable in double curly braces:

await mailslurp.createTemplate({
  name: "Welcome email",
  content: "Hello {{firstName}}. Welcome to {{brandName}}."
});

MailSlurp will recognize the firstName and brandName variables which can then be supplied as a map of templateVariables in sendEmail() methods. They will also be displayed in the dashboard:

teplates

# Passing variable values

When you send an email with a template you can pass a map of template variables and values.

await mailslurp.sendEmail(inbox.id, {
  to: [''],
  template: templateId
  templateVariables: {
    firstName: '',
    brandName: ''
  }
})

MailSlurp will validate emails and return an error if passed variable names do not match template variables.

# Contacts and groups

MailSlurp also supports contact and contact groups. You can create these in the dashboard.

To send an email to a contact use the toContacts property:

await mailslurp.sendEmail(inbox.id, { toContacts: [contactId1, contactId2] });

To send emails to each contact in a group use the toGroup property.

await mailslurp.sendEmail(inbox.id, { toGroup: groupId });