![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
The Mailosaur Node library lets you integrate email and SMS testing into your continuous integration process.
Mailosaur is a service that allows you to test email and SMS messages by capturing them in a virtual inbox. This is particularly useful for automated testing of email and SMS functionalities in your applications.
Capture Emails
This feature allows you to capture and list emails sent to a specific Mailosaur server. You can use this to verify that emails are being sent correctly during automated tests.
const MailosaurClient = require('mailosaur');
const client = new MailosaurClient('YOUR_API_KEY');
client.messages.list('SERVER_ID').then(result => {
console.log(result.items);
});
Retrieve Email Content
This feature allows you to retrieve the content of a specific email, including the subject and body. This is useful for verifying the content of emails in your tests.
const MailosaurClient = require('mailosaur');
const client = new MailosaurClient('YOUR_API_KEY');
client.messages.get('SERVER_ID', 'EMAIL_ID').then(email => {
console.log(email.subject);
console.log(email.text.body);
});
Search Emails
This feature allows you to search for emails based on specific criteria, such as the recipient's email address. This can be useful for finding specific emails in a large inbox.
const MailosaurClient = require('mailosaur');
const client = new MailosaurClient('YOUR_API_KEY');
client.messages.search('SERVER_ID', { sentTo: 'test@example.com' }).then(result => {
console.log(result.items);
});
Capture SMS
This feature allows you to capture and list SMS messages sent to a specific Mailosaur server. This is useful for testing SMS functionalities in your applications.
const MailosaurClient = require('mailosaur');
const client = new MailosaurClient('YOUR_API_KEY');
client.messages.list('SERVER_ID', { type: 'sms' }).then(result => {
console.log(result.items);
});
Mailinator is a service that provides disposable email addresses for testing purposes. It allows you to capture and read emails sent to these addresses. Compared to Mailosaur, Mailinator is more focused on providing temporary email addresses for quick testing rather than comprehensive email and SMS testing.
Ethereal Email is a service that provides a fake SMTP service for testing email sending capabilities. It allows you to capture and inspect emails sent during development and testing. Unlike Mailosaur, Ethereal Email is primarily focused on email and does not support SMS testing.
Mailtrap is a service that provides a safe environment for testing email sending capabilities. It captures emails sent from your application and allows you to inspect them. Mailtrap is similar to Mailosaur in that it provides a virtual inbox for testing, but it does not support SMS testing.
Mailosaur lets you automate email and SMS tests as part of software development and QA.
This guide provides several key sections:
You can find the full Mailosaur documentation on the website.
If you get stuck, just contact us at support@mailosaur.com.
Install the Mailosaur Node.js library via npm or yarn:
npm i -D mailosaur
# or
yarn add -D mailosaur
Then import the library into your code. The value for YOUR_API_KEY
is covered in the next step (creating an account):
const MailosaurClient = require('mailosaur')
const mailosaur = new MailosaurClient('YOUR_API_KEY')
This library is powered by the Mailosaur email & SMS testing API. You can easily check out the API itself by looking at our API reference documentation or via our Postman or Insomnia collections:
Create a free trial account for Mailosaur via the website.
Once you have this, navigate to the API tab to find the following values:
Mailosaur gives you an unlimited number of test email addresses - with no setup or coding required!
Here's how it works:
abc123.mailosaur.net
)@{YOUR_SERVER_DOMAIN}
will work with Mailosaur without any special setup. For example:
build-423@abc123.mailosaur.net
john.smith@abc123.mailosaur.net
rAnDoM63423@abc123.mailosaur.net
Can't use test email addresses? You can also use SMTP to test email. By connecting your product or website to Mailosaur via SMTP, Mailosaur will catch all email your application sends, regardless of the email address.
In automated tests you will want to wait for a new email to arrive. This library makes that easy with the messages.get
method. Here's how you use it:
(async () => {
const MailosaurClient = require('mailosaur')
const mailosaur = new MailosaurClient('API_KEY')
// See https://mailosaur.com/app/project/api
const serverId = 'abc123'
const serverDomain = 'abc123.mailosaur.net'
const searchCriteria = {
sentTo: 'anything@' + serverDomain
}
const message = await mailosaur.messages.get(serverId, searchCriteria)
console.log(message.subject) // "Hello world!"
})()
MailosaurClient
with your API key.abc123
.First, check that the email you sent is visible in the Mailosaur Dashboard.
If it is, the likely reason is that by default, messages.get
only searches emails received by Mailosaur in the last 1 hour. You can override this behavior (see the receivedAfter
option below), however we only recommend doing this during setup, as your tests will generally run faster with the default settings:
const email = await mailosaur.messages.get(
serverId,
searchCriteria,
// Override receivedAfter to search all messages since Jan 1st
{ receivedAfter: new Date(2021, 01, 01) }
)
Important: Trial accounts do not automatically have SMS access. Please contact our support team to enable a trial of SMS functionality.
If your account has SMS testing enabled, you can reserve phone numbers to test with, then use the Mailosaur API in a very similar way to when testing email:
(async () => {
const MailosaurClient = require('mailosaur')
const mailosaur = new MailosaurClient('API_KEY')
const serverId = 'abc123'
const searchCriteria = {
sentTo: '4471235554444'
}
const sms = await mailosaur.messages.get(serverId, searchCriteria)
console.log(sms.text.body)
})()
Most emails, and all SMS messages, should have a plain text body. Mailosaur exposes this content via the text.body
property on an email or SMS message:
console.log(message.text.body) // "Hi Jason, ..."
if (message.text.body.indexOf('Jason') > -1) {
console.log('Email contains "Jason"')
}
You may have an email or SMS message that contains an account verification code, or some other one-time passcode. You can extract content like this using a simple regex.
Here is how to extract a 6-digit numeric code:
console.log(message.text.body) // "Your access code is 243546."
const regEx = new RegExp('([0-9]{6})')
const matches = regEx.exec(message.text.body)
console.log(matches[0]) // "243546"
Most emails also have an HTML body, as well as the plain text content. You can access HTML content in a very similar way to plain text:
console.log(message.html.body) // "<html><head ..."
If you need to traverse the HTML content of an email. For example, finding an element via a CSS selector, you can use the JSDOM library.
npm i -D jsdom
# or
yarn add -D jsdom
const { JSDOM } = require('jsdom')
// ...
const dom = new JSDOM(message.html.body);
const el = dom.window.document.querySelector('.verification-code');
const verificationCode = el.textContent; // "542163"
When an email is sent with an HTML body, Mailosaur automatically extracts any hyperlinks found within anchor (<a>
) and area (<area>
) elements and makes these viable via the html.links
array.
Each link has a text property, representing the display text of the hyperlink within the body, and an href property containing the target URL:
// How many links?
console.log(message.html.links.length) // 2
const firstLink = message.html.links[0]
console.log(firstLink.text) // "Google Search"
console.log(firstLink.href) // "https://www.google.com/"
Important: To ensure you always have valid emails. Mailosaur only extracts links that have been correctly marked up with <a>
or <area>
tags.
Mailosaur auto-detects links in plain text content too, which is especially useful for SMS testing:
// How many links?
console.log(message.text.links.length) // 2
const firstLink = message.text.links[0]
console.log(firstLink.href) // "https://www.google.com/"
If your email includes attachments, you can access these via the attachments
property:
// How many attachments?
console.log(message.attachments.length) // 2
Each attachment contains metadata on the file name and content type:
const firstAttachment = message.attachments[0]
console.log(firstAttachment.fileName) // "contract.pdf"
console.log(firstAttachment.contentType) // "application/pdf"
The length
property returns the size of the attached file (in bytes):
const firstAttachment = message.attachments[0]
console.log(firstAttachment.length) // 4028
const fs = require('fs')
// ...
const firstAttachment = message.attachments[1]
const fileBytes = await mailosaur.files.getAttachment(firstAttachment.id)
fs.writeFileSync(firstAttachment.fileName, fileBytes)
The html.images
property of a message contains an array of images found within the HTML content of an email. The length of this array corresponds to the number of images found within an email:
// How many images in the email?
console.log(message.html.images.length) // 1
Emails will often contain many images that are hosted elsewhere, such as on your website or product. It is recommended to check that these images are accessible by your recipients.
All images should have an alternative text description, which can be checked using the alt
attribute.
const image = message.html.images[0]
console.log(image.alt) // "Hot air balloon"
A web beacon is a small image that can be used to track whether an email has been opened by a recipient.
Because a web beacon is simply another form of remotely-hosted image, you can use the src
attribute to perform an HTTP request to that address:
const https = require('https')
// ...
const image = message.html.images[0]
console.log(image.src) // "https://example.com/s.png?abc123"
// Make an HTTP call to trigger the web beacon
https.get(image.src, r => console.log(r.statusCode)) // 200
You can perform a SpamAssassin check against an email. The structure returned matches the spam test object:
const result = await mailosaur.analysis.spam(message.id)
console.log(result.score) // 0.5
result.spamFilterResults.spamAssassin.forEach(r => {
console.log(r.rule)
console.log(r.description)
console.log(r.score)
})
If you'd like to contribute to this library, here is how to set it up locally.
Install all development dependencies:
npm i
The test suite requires the following environment variables to be set:
export MAILOSAUR_API_KEY=your_api_key
export MAILOSAUR_SERVER=server_id
Run all tests:
npm test
You can get us at support@mailosaur.com
FAQs
The Mailosaur Node library lets you integrate email and SMS testing into your continuous integration process.
The npm package mailosaur receives a total of 139,723 weekly downloads. As such, mailosaur popularity was classified as popular.
We found that mailosaur demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.