/Test Cases

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

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

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, ..."
print(message.text.body) # "Hi Jason, ..."
System.out.println(message.text().body()); // "Hi Jason, ..."
Console.WriteLine(message.Text.Body); // "Hi Jason, ..."
puts(message.text.body) # "Hi Jason, ..."
print($message->text->body); // "Hi Jason, ..."
fmt.Println(message.Text.Body) // "Hi Jason, ..."

Working with the content

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

const containsText = message.text.body.indexOf('Jason') > -1
console.log(containsText) // true
contains_text = "Jason" in message.text.body
print(contains_text) # true
boolean containsText = message.text().body().contains("Jason");
System.out.println(containsText); // true
Console.WriteLine(message.Text.Body.Contains("Jason")); // true
contains_text = message.text.body.include? "Jason"
puts(contains_text) # true
$containsText = strpos($message->text->body, 'Jason') !== false;
print($containsText); // 1
containsText := strings.Contains(message.Text.Body, "Jason")
fmt.Println(containsText) // true

Finding a 6-digit code

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"
print(message.text.body) # "Your access code is 243546."

match = re.search("([0-9]{6})", message.text.body)
print(match.group()) # "243546"
System.out.println(message.text().body()); // "Your access code is 243546."

Pattern pattern = Pattern.compile(".*([0-9]{6}).*");
Matcher matcher = pattern.matcher(message.text().body());
matcher.find();

System.out.println(matcher.group(1)); // "243546"
Console.WriteLine(message.Text.Body); // "Your access code is 243546."

var matches = Regex.Matches(message.Text.Body, "([0-9]{6})", RegexOptions.IgnoreCase);

Console.WriteLine(matches[0]); // "243546"
puts(message.text.body) # "Your access code is 243546."

match = message.text.body.match(/([0-9]{6}))
puts(match[0]) # "243546"
print($message->text->body); // "Your access code is 243546."

preg_match('/([0-9]){6}/', $message->text->body, $matches);
print($matches[0]); // "243546"
fmt.Println(message.Text.Body) // "Your access code is 243546."

r, _ := regexp.Compile("([0-9]{6})")
fmt.Println(r.FindString(message.Text.Body)) // "243546"

Finding an 8-digit alphanumeric code

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"
print(message.text.body) # "Your access code is QZ524822."

match = re.search("([A-Z]{2}[0-9]{6})", message.text.body)
print(match.group()) # "Q2ZH482S"
System.out.println(message.text().body()); // "Your access code is QZ524822."

Pattern pattern = Pattern.compile(".*([A-Z]{2}[0-9]{6}).*");
Matcher matcher = pattern.matcher(message.text().body());
matcher.find();

System.out.println(matcher.group(1)); // "Q2ZH482S"
Console.WriteLine(message.Text.Body); // "Your access code is QZ524822."

var matches = Regex.Matches(message.Text.Body, "([A-Z]{2}[0-9]{6})", RegexOptions.IgnoreCase);

Console.WriteLine(matches[0]); // "Q2ZH482S"
puts(message.text.body) # "Your access code is QZ524822."

match = message.text.body.match(/([A-Z]{2}[0-9]{6}))
puts(match[0]) # "Q2ZH482S"
print($message->text->body); // "Your access code is QZ524822."

preg_match('/([0-9]){6}/', $message->text->body, $matches);
print($matches[0]); // "Q2ZH482S"
fmt.Println(message.Text.Body) // "Your access code is QZ524822."

r, _ := regexp.Compile("([A-Z]{2}[0-9]{6})")
fmt.Println(r.FindString(message.Text.Body)) // "Q2ZH482S"