Gathering detailed insights and metrics for mailosaur
Gathering detailed insights and metrics for mailosaur
Gathering detailed insights and metrics for mailosaur
Gathering detailed insights and metrics for mailosaur
npm install mailosaur
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
20 Stars
171 Commits
13 Forks
3 Watching
1 Branches
9 Contributors
Updated on 21 Nov 2024
JavaScript (98.13%)
HTML (1.87%)
Cumulative downloads
Total Downloads
Last day
-19.8%
27,556
Compared to previous day
Last week
3%
165,896
Compared to previous week
Last month
4.3%
675,411
Compared to previous month
Last year
33.1%
6,720,315
Compared to previous year
1
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:
1npm i -D mailosaur 2# or 3yarn 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):
1const MailosaurClient = require('mailosaur') 2const 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:
1(async () => { 2 const MailosaurClient = require('mailosaur') 3 const mailosaur = new MailosaurClient('API_KEY') 4 5 // See https://mailosaur.com/app/project/api 6 const serverId = 'abc123' 7 const serverDomain = 'abc123.mailosaur.net' 8 9 const searchCriteria = { 10 sentTo: 'anything@' + serverDomain 11 } 12 13 const message = await mailosaur.messages.get(serverId, searchCriteria) 14 15 console.log(message.subject) // "Hello world!" 16})()
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:
1const email = await mailosaur.messages.get( 2 serverId, 3 searchCriteria, 4 // Override receivedAfter to search all messages since Jan 1st 5 { receivedAfter: new Date(2021, 01, 01) } 6)
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:
1(async () => { 2 const MailosaurClient = require('mailosaur') 3 const mailosaur = new MailosaurClient('API_KEY') 4 5 const serverId = 'abc123' 6 7 const searchCriteria = { 8 sentTo: '4471235554444' 9 } 10 11 const sms = await mailosaur.messages.get(serverId, searchCriteria) 12 13 console.log(sms.text.body) 14})()
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:
1console.log(message.text.body) // "Hi Jason, ..." 2 3if (message.text.body.indexOf('Jason') > -1) { 4 console.log('Email contains "Jason"') 5}
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:
1console.log(message.text.body) // "Your access code is 243546." 2 3const regEx = new RegExp('([0-9]{6})') 4const matches = regEx.exec(message.text.body) 5 6console.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:
1console.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.
1npm i -D jsdom 2# or 3yarn add -D jsdom
1const { JSDOM } = require('jsdom') 2 3// ... 4 5const dom = new JSDOM(message.html.body); 6 7const el = dom.window.document.querySelector('.verification-code'); 8const 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:
1// How many links? 2console.log(message.html.links.length) // 2 3 4const firstLink = message.html.links[0] 5console.log(firstLink.text) // "Google Search" 6console.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:
1// How many links? 2console.log(message.text.links.length) // 2 3 4const firstLink = message.text.links[0] 5console.log(firstLink.href) // "https://www.google.com/"
If your email includes attachments, you can access these via the attachments
property:
1// How many attachments? 2console.log(message.attachments.length) // 2
Each attachment contains metadata on the file name and content type:
1const firstAttachment = message.attachments[0] 2console.log(firstAttachment.fileName) // "contract.pdf" 3console.log(firstAttachment.contentType) // "application/pdf"
The length
property returns the size of the attached file (in bytes):
1const firstAttachment = message.attachments[0] 2console.log(firstAttachment.length) // 4028
1const fs = require('fs') 2 3// ... 4 5const firstAttachment = message.attachments[1] 6 7const fileBytes = await mailosaur.files.getAttachment(firstAttachment.id) 8fs.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:
1// How many images in the email? 2console.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.
1const image = message.html.images[0] 2console.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:
1const https = require('https') 2 3// ... 4 5const image = message.html.images[0] 6console.log(image.src) // "https://example.com/s.png?abc123" 7 8// Make an HTTP call to trigger the web beacon 9https.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:
1const result = await mailosaur.analysis.spam(message.id) 2 3console.log(result.score) // 0.5 4 5result.spamFilterResults.spamAssassin.forEach(r => { 6 console.log(r.rule) 7 console.log(r.description) 8 console.log(r.score) 9})
If you'd like to contribute to this library, here is how to set it up locally.
Install all development dependencies:
1npm i
The test suite requires the following environment variables to be set:
1export MAILOSAUR_API_KEY=your_api_key 2export MAILOSAUR_SERVER=server_id
Run all tests:
1npm test
You can get us at support@mailosaur.com
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More