Mailosaur logo
Mailosaur logo

How to test the text content of email or SMS

Learn how to test the text content of email or SMS with Mailosaur.

HTML and text content within an email Most email sent today is HTML-based, however an email will often contain a plain text alternative for older mail clients, which is what this page relates to. Learn how to work with the HTML content of an email.

Before you begin
A hashtag icon

The examples shown below are based on two key assumptions:

  1. That you have already create a basic automated test using our getting started guides.
  2. You have a chosen assertion library that you will use to test the values shown below.

Accessing the text
A hashtag icon

The text content of email or SMS message can be accessed via the text.body property of a message.

console.log(message.text.body) // "Hi Jason, ..."

Working with the content
A hashtag icon

This section describes some of the most common use cases related to the content of an email or SMS message. You should also refer to the guides on working with automatically-extracted content such as links and images.

Checking if message contains a string
A hashtag icon

const containsText = message.text.body.indexOf('Jason') > -1
console.log(containsText) // true

Finding a 6-digit code
A hashtag icon

Note: We recommend using Mailosaur’s built-in verification code detection. However, you can also write your own regular expressions to extract values from within your content.

The example below shows how to extract a 6-digit numeric code from a message.

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"

Finding an 8-digit alphanumeric code
A hashtag icon

In this example, an 8-digit code, consisting of 2 letters, followed by 6 numbers is extracted.

console.log(message.text.body) // "Your access code is QZ524822."

const regEx = new RegExp('([A-Z]{2}[0-9]{6})')
const matches = regEx.exec(message.text.body)

console.log(matches[0]) // "Q2ZH482S"