# How to send emails with MailJet

# What is MailJet

MailJet is a SAAS API for sending transactional emails. These emails are usually triggered by your application in response to a particular event. An example is sending an email to a new user when they sign-up.

# What does it solve?

Services like MailJet make sending emails easy by abstracting SMTP protocols behind HTTP APIs, SDKs, and client libraries. This means you can concentrate on the emails you send not on how you send them.

# Sending an email with MailJet

MailJet has great API documentation (opens new window) and many official libraries. You can use MailJet with any language but for this article let's see how to send an email using the Node/Javascript library.

First add MailJet as a dependency in the node project. npm install -s node-mailjet.

Then to send an email create an MailJet instance and invoke a send request on it.

const mailjet = require("node-mailjet").connect(
function sendEmail(recipient) {
  return mailjet
    .post("send", { version: "v3.1" })
      Messages: [
          From: {
            Email: "support@your-domain.com",
            Name: "your-application-name",
          To: [
              Email: recipient,
          Subject: "",
          TextPart: "",
          HTMLPart: "",
    .then((result) => {
      // do something with the send result or ignore
    .catch((err) => {
      // handle an error

# Email contents

The simplest transactional email you can send will simply have one recipient, a subject line, and some text. Most users want more however and will send HTML emails with images, links, and style. For the example above one might design email HTML in a separate file and load that into the function with nodes fs.readFile() function.

HTML in emails can be complicated. Keep it simple (real simple!) and your emails should render correctly for most people.

# Test and receive email

To test your MailJet implementation you can trigger your code with your personal email address as the recipient. If you receive an email you know that your code works. But how do we add this type of test to our automated test suite?

MailSlurp is a service that lets you generate email addresses (opens new window) in code and then receive test emails with them. It has an official Node library so let's use that.

First you need to sign up (opens new window) and get an API Key. mailslurp api key

Now lets add the MailSlurp client as a dependency npm install --save-dev mailslurp-client.

In your tests first create a MailSlurp client.

import { MailSlurp } from "mailslurp-client";
const api = new MailSlurp({ apiKey: "your-mailslurp-api-key" });

Then create a new email address.

const newEmailInbox = await api.createInbox();

Now invoke your MailJet sending function with the address you created

const result = await sendEmail(newEmailInbox.emailAddress);

By integrating the code above into our application's test suite we can end to end test email sending by capturing the results with MailSlurps test email accounts.