Comparing email previews providers? Discover our new pricing options - chat to sales or book a demo to unlock your savings now
AutomationCodes

Extracting codes from email and SMS

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

Before you begin

The examples shown below are based on two key assumptions:

  1. You have already created 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.

Codes within email and SMS text content

When an email or SMS message is sent with plain text content, Mailosaur automatically extracts anything that looks 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"
# How many codes?
print(len(message.text.codes)) # 2

first_code = message.text.codes[0]
print(first_code.value) # "456812"
// How many codes?
System.out.println(message.text().codes().size()); // 2

Code firstCode = message.text().codes().get(0);
System.out.println(firstCode.value()); // "456812"
// How many codes?
Console.WriteLine(message.Text.Codes.Count); // 2

var firstCode = message.Text.Codes[0];
Console.WriteLine(firstCode.Value); // "456812"
# How many codes?
puts(message.text.codes.length) # 2

first_code = message.text.codes[0]
puts(first_code.value) # "456812"
// How many codes?
print(count($message->text->codes)); // 2

$firstCode = $message->text->codes[0];
print($firstCode->value); // "456812"
// How many codes?
fmt.Println(len(message.Text.Codes)) // 2

firstCode := message.Text.Codes[0]
fmt.Println(firstCode.Value) // "456812"

Codes within email HTML content

As with codes 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 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"
# How many codes?
print(len(message.html.codes)) # 2

first_code = message.html.codes[0]
print(first_code.value) # "456812"
// How many codes?
System.out.println(message.html().codes().size()); // 2

Code firstCode = message.html().codes().get(0);
System.out.println(firstCode.value()); // "456812"
// How many codes?
Console.WriteLine(message.Html.Codes.Count); // 2

var firstCode = message.Html.Codes[0];
Console.WriteLine(firstCode.Value); // "456812"
# How many codes?
puts(message.html.codes.length) # 2

first_code = message.html.codes[0]
puts(first_code.value) # "456812"
// How many codes?
print(count($message->html->codes)); // 2

$firstCode = $message->html->codes[0];
print($firstCode->value); // "456812"
// How many codes?
fmt.Println(len(message.Html.Codes)) // 2

firstCode := message.Html.Codes[0]
fmt.Println(firstCode.Value) // "456812"

What if my code is not detected?

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

See also