Mailosaur logo
Mailosaur logo

Extracting codes from email and SMS

Learn how to test verification codes found in email and SMS messages with Mailosaur.

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.

When an email or SMS message is sent with plain text content, Mailosaur automatically extracts anything that look like a verification code and makes these available via the text.codes array. Each code has a value property, representing the code itself.

// How many codes?
console.log(message.text.codes.length) // 2

const firstCode = message.text.codes[0]
console.log(firstCode.value) // "456812"

As with links found within the text content of a message, Mailosaur also detects and extracts codes found within the HTML of any email.

These are made available viable via the html.codes array.

// How many codes?
console.log(message.html.codes.length) // 2

const firstCode = message.html.codes[0]
console.log(firstCode.value) // "456812"

What if my code is not detected?
A hashtag icon

Mailosaur automatically detects most common verification code patterns, however you can still write your own regular expressions to meet more specific requirements.