Send email with Axios JS (and receive email too!)

Send email in Javascript with MailSlurp email API

Axios JS sending emails Axios is a popular HTTP request library for Javascript and NodeJS. It lets you fetch webpages and call APIs from the browser and NodeJS in a reliable and friendly way. With a MailSlurp account Axios can also send and receive emails!

What is Axios?

You can install Axios from NPM as follows:

npm install axios

Making a request is easy once installed:

const axios = require("axios");
axios
  .get("/user?ID=12345")
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

What is MailSlurp?

MailSlurp is a free email API. You can create email addresses on demand then send and receive emails from Javascript. You can create a free account then use the API Key to send and receive emails with Axios.

How to ues email with Axios

To use MailSlurp with Axios first import the library and your API Key.

const axios = require("axios");
const API_KEY = "your-mailslurp-api-key";

Create an inbox

First we can create an email address like so:

async function createInbox() {
  // call MailSlurp createInbox endpoint
  return await axios
    .post(`https://api.mailslurp.com/createInbox?apiKey=${API_KEY}`)
    .then((res) => res.data);
}

MailSlurp inboxes have real email addresses than can send and receive email.

{
  "id": "123",
  "emailAddress": "123@mailslurp.com"
}

Send an email

Sending an email is easy with Axios and MailSlurp. Simple POST data to https://api.mailslurp.com/sendEmail. Include your API Key as a query parameter: ?apiKey=your-api-key.

// send email from inbox 1 to inbox 2
// NOTE you can send emails to any address with MailSlurp
await axios({
  method: "POST",
  url: `https://api.mailslurp.com/sendEmail?apiKey=${API_KEY}`,
  data: {
    senderId: inbox1.id,
    to: inbox2.emailAddress,
    subject: "Hello inbox 2",
    body: "Test from inbox 1",
  },
});

Receive emails

You can receive emails with MailSlurp too!

// receive the email from inbox 2
const email = await axios
  .get(
    `https://api.mailslurp.com/waitForLatestEmail?apiKey=${API_KEY}&inboxId=${inbox2.id}`
  )
  .then((res) => res.data);
expect(email.from).toEqual(inbox1.emailAddress);
expect(email.subject).toEqual("Hello inbox 2");
expect(email.body).toEqual("Test from inbox 1");

Next steps

You can use Axios and MailSlurp for all your email needs. Sending, receiving, or even end-to-end testing.

Related content

API Reference

Build powerful applications and test suites using real email addresses in code. Create inboxes on demand then send and receive emails and attachments in Javascript, Go, PHP, Ruby, Java, C#, REST and more.

Official MailSlurp documentation

Developer documentation and guides for MailSlurp's email APIs. REST, SDKS, Graphql, SMTP and more. MailSlurp is an email and SMS API service for develop

NodeJS MailSlurp SDK

Nodemailer alternatives to send and receieve email in code and tests

Ruby Mailer SDK - send and receive emails in Rails, Rspec and more using MailSlurp's official email Gem

Receive email and attachments with Rails, Rspec, and Ruby without SMTP using MailSlurp Ruby Gem.

Temporary email api

Temporary email API for disposable email accounts. Free for personal use with REST API and clients in NodeJS, CSharp and more.

Fastest way to start a typescript project

Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.

Fetch error message javascript

Handle response exceptions with Fetch in Javascript

Mailinator alternative

Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp

CypressJS Example

Test email sign-up. password verification and more with Cypress JS and MailSlurp.

CypressJS Email Testing

Use real email accounts in CypressJS to test user sign-up, email verification, and more.

Generate API clients using Swagger

How to create SDK libraries a language of your choice

Send email with Axios JS (and receive email too!)

Send email in Javascript with MailSlurp email API

Jest, Puppeteer

Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.

Webdriver, JS, WDIO

Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.

Deno Email Apis

Send and receive emails in Deno JS. Use APIs in Deno with MailSlurp.

Using Attachments

Use MailSlurp's attachment endpoints to upload, send, receive, and download attachments.

MailSlurp NodeMailer SMTP Usage

Use SMTP nodemailer with MailSlurp disposable email addresses

Using custom domains with MailSlurp

Email custom domains with API access

Create email inboxes using MailSlurp's API

Create email inboxes using MailSlurp's API

MailSlurp API entities and how to use them

MailSlurp API entities and how to use them

Get emails from code (how to receive emails programmatically)

How to receive emails directly in code, applications, and tests. Use MailSlurp to send and receive SMTP emails.

Sending email in Javascript and NodeJS (Typescript examples)

Sending email in Javascript and NodeJS (Typescript examples)

Testing Email with Cypress JS and MailSlurp

Email testing with Cypress JS

Ready to dive in?Start building email applications today.