Automated Email Testing in 5 Minutes! Send and receive emails in CI and code.

Image for post
Image for post
Email APIs let you create email addresses on demand during automated tests.

Why?

Basics

Environments

npm install --save mailslurp-client
const MailSlurp = require("mailslurp-client").default;
import { MailSlurp } from "mailslurp-client";
const mailSlurp = new MailSlurp({ apiKey: "your_key_here" });

Common use cases

Creating inboxes

const inbox = await mailslurp.createInbox();
{
"id": "123",
"emailAddress": "123@mailslurp.com"
}

Sending emails

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

Receiving emails

waitForLatestEmail(inboxId?: string, timeout?: number): Promise<Email>
test('app sends welcome`, async () => {

const inbox = await mailslurp.createInbox();

await myApp.sendWelcome(inbox.emailAddress);

const welcome = await mailslurp.waitForLatestEmail(inbox.id)

expect(welcome.subject).toBe('Welcome!')
})

Extracting email content

const email = await mailslurp.waitForLatestEmail(inbox.id);
console.log(email.body);
// fetch an email
const email = await mailslurp.waitForLatestEmail(inbox.id);

// execute a regular express capture group on the body and
// destructure the matching group into a variable
const [_, verificationCode] = /your code is "([0-9]{6})"/g.exec(email.body);

// do something with code like verifying an account
myApp.confirmUser(verificationCode);

Special characters

// notice that "=" is replaced with "&#x3D;" HTML entity
const [_, code] = /\?code&#x3D;([^'"]+)/g.exec(body);

Test Framework integrations

CypressJS

{
"defaultCommandTimeout": 30000,
"requestTimeout": 30000
}
const { MailSlurp } = require("mailslurp-client");

Cypress.Commands.add("createInbox", () => {
return mailslurp.createInbox();
});
it("can sign up", () => {
cy.createInbox((inbox) => {
cy.get("#sign-up-email").type(inbox.emailAddress);
// etc
});
});

Next steps

Test Email API for end-to-end with real email addresses. Support for NodeJS, PHP, Python, Ruby, Java, C# and more. See https://www.mailslurp.com for details.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store