Extracting links from email and SMS

Learn how to test hyperlinks found in email and SMS messages with Mailosaur

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.

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.

Note that only links that have been correctly marked up in HTML will be detected.

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

const firstLink = message.html.links[0]
console.log(firstLink.text) // "Google Search"
console.log(firstLink.href) // "https://www.google.com/"
# How many links?
print(len(message.html.links)) # 2

first_link = message.html.links[0]
print(first_link.text) # "Google Search"
print(first_link.href) # "https://www.google.com/"
// How many links?
System.out.println(message.html().links().size()); // 2

Link firstLink = message.html().links().get(0);
System.out.println(firstLink.text()); // "Google Search"
System.out.println(firstLink.href()); // "https://www.google.com/"
// How many links?
Console.WriteLine(message.Html.Links.Count); // 2

var firstLink = message.Html.Links[0];
Console.WriteLine(firstLink.Text); // "Google Search"
Console.WriteLine(firstLink.Href); // "https://www.google.com/"
# How many links?
puts(message.html.links.length) # 2

first_link = message.html.links[0]
puts(first_link.text) # "Google Search"
puts(first_link.href) # "https://www.google.com/"
// How many links?
print(count($message->html->links)); // 2

$firstLink = $message->html->links[0];
print($firstLink->text); // "Google Search"
print($firstLink->href); // "https://www.google.com/"
// How many links?
fmt.Println(len(message.Html.Links)) // 2

firstLink := message.Html.Links[0]
fmt.Println(firstLink.Text) // "Google Search"
fmt.Println(firstLink.Href) // "https://www.google.com/"

As with links found within the HTML content of an email, Mailosaur also detects and extracts links found within the plain text of a message (both email and SMS).

These are made available viable via the text.links array.

Each link has an href property containing the target URL. Whilst each link also has a text property, this will always have the same value as href.

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

const firstLink = message.text.links[0]
console.log(firstLink.href) // "https://www.google.com/"
# How many links?
print(len(message.text.links)) # 2

first_link = message.text.links[0]
print(first_link.href) # "https://www.google.com/"
// How many links?
System.out.println(message.text().links().size()); // 2

Link firstLink = message.text().links().get(0);
System.out.println(firstLink.text()); // "Google Search"
System.out.println(firstLink.href()); // "https://www.google.com/"
// How many links?
Console.WriteLine(message.Text.Links.Count); // 2

var firstLink = message.Text.Links[0];
Console.WriteLine(firstLink.Href); // "https://www.google.com/"
# How many links?
puts(message.text.links.length) # 2

first_link = message.text.links[0]
puts(first_link.href) # "https://www.google.com/"
// How many links?
print(count($message->text->links)); // 2

$firstLink = $message->text->links[0];
print($firstLink->href); // "https://www.google.com/"
// How many links?
fmt.Println(len(message.Text.Links)) // 2

firstLink := message.Text.Links[0]
fmt.Println(firstLink.Href) // "https://www.google.com/"