Testim Copilot is here | Learn More

Testing Mailto Links: Tutorial With Example

Ever wanted more of your site's users to email you? Or maybe they're emailing you, but it would be better…

Testim
By Testim,

Ever wanted more of your site’s users to email you? Or maybe they’re emailing you, but it would be better if they followed a specific format? Mailto links are an easy way to help your users send email messages.

In this post, you’ll learn what mailto links are and when you’d want to use them. Then, we’ll share some ways to test mailto links using standard browser testing tools. Even if you’re already familiar with these links, this post may give you some ideas on using them more effectively.

First of all, let’s define a primary term.

What Are Mailto Links?

Mailto links are a Universal Resource Identifier (URI) used in HTML link tags. Mailto links open a new email in the email client when a user clicks them. You can specify a message and subject in the URI—these will automatically be in the draft email. Mailto links are excellent if you need people who email you to follow a specific format, as in bug reports.

Expand Your Test Coverage

Fast and flexible authoring of AI-powered end-to-end tests — built for scale.
Start Testing Free

Why Use Mailto Links?

Smaller businesses, such as Realtors, often use mailto on public websites to coordinate inbound clients through email. Embedding a mailto link that automatically opens a blank email can be a great way to speed potential prospects down a sales funnel. You can also automatically populate the BCC and CC fields in an email. And that’s helpful if you want an email to go to additional people or possibly a mailing list.

How else can you use these links? Well, mailto links are also helpful for email archives. For example, you can use a mailto link to pre-configure a reply to an email thread. Also, using the In-Reply-To field, you can populate the previous message in a thread.

Some mailing list programs subscribe or unsubscribe to an email address in response to an email that contains the word “subscribe.” What’s more, you can provide a mailto link to give users an easy way to subscribe or unsubscribe from a webpage.

Useful, But Just a Suggestion

Mailto links don’t actually send emails. Instead, all they do is open up a draft email with pre-populated fields. After that, the user can edit and send any message they wish. Mailto links are just a suggestion for the users.

Now that you know the basics let’s explore further.

When Would You Want to Test a Mailto Link?

Case 1: Often, mailto links are just static “Contact Us” messages that don’t change. However, if the rest of your website is evolving, you’ll want a test to ensure that your mailto link doesn’t inadvertently disappear. Otherwise, you might not notice until you stop getting emails from customers!

Case 2: You may want to dynamically generate mailto links depending on user data. In this case, it’s important to test across various user types to ensure that each one generates a link correctly.

What Tools to Use to Test Mailto Links

Mailto links are a little unusual in the web testing world because instead of opening a new webpage, they open a separate application. This makes it challenging to validate the contents of the email with our standard web test automation tools. For example, we can take photographs of the current browser state using Selenium. But it won’t capture the email draft that the mailto link triggers. That’s because the email client is a different process from the browser.

One solution is to use desktop testing tools. These focus on testing desktop applications and aren’t limited to a simple browser instance. For example, Microsoft provides a library WinAppDriver that allows you to use a Selenium-style interface to interact with Windows applications.

But since mailto links are just regular HTML, we can validate the contents of our mailto link using standard, browser-based testing and still be confident in our code. That lets us use standard, headless, browser-based testing tools like Selenium or Testim to validate our webpage.

How to Test Mailto Links

For our example, we have a documentation page with a mailto link encouraging customers to submit bug reports. We automatically include the customer identification and root user email for logged-in customers to ensure they don’t forget to include it in their bug report. Since we’re generating these mailto links dynamically based on the customer, we risk that something will change in our data model or front-end code that could break the link.

Next, let’s take a quick look at the structure of mailto links.

<a href="mailto:[email protected]?subject=Customer Question&body=Include your questions below.">Contact Us</a>

Looking at the mailto link above, we see that it’s just an HTML link or a tag with an HREF field set to a mailto URI. The key component of the HREF is the first part, mailto:[email protected]. As long as we have the mailto header and the first email address, our link should open the customer’s email client successfully.

As mentioned previously, mailto also allows you to set the CC and BCC fields if you want to copy the email to additional recipients. And you have the option of specifying multiple recipients, as you can see in the example below.

<a href="mailto:[email protected],[email protected],[email protected][email protected]&[email protected]&subject=Customer Question&body=Include your questions below.">Contact Us</a>

Testing Mailto Links With Static Content

I created a Ruby on Rails example for this tutorial. You can reference it at https://github.com/Sevii/MailToTesting. Inside that repository, I’ve set up a Rails system test using Selenium.

To validate a basic, static mailto link, the test first visits our home page, finds a link tag with the text “Contact Us,” and finally validates the HREF of that link tag. We’re using the Ruby Selenium wrapper here. Check out this beginner’s walkthrough if you want a primer on getting started with Selenium and JavaScript.

require "application_system_test_case"
require "selenium-webdriver"

class DocsTest < ApplicationSystemTestCase
  test "visiting the docs page" do
    visit '/'
    element = page.find_link(:text => "Contact Us")
    href = element['href']
    assert href.include?("mailto:[email protected]")
  end
end

This test is enough for a static mailto link to confirm it’s working correctly. We’re validating that the link exists and has the key components needed to populate an email.

But what about dynamic content? Let’s tackle that next.

Testing Mailto Links With Dynamic Content

Next, we’ll look at an example that validates dynamically generated fields in a mailto link. We’re embedding the userId and root account email into our mailto link for this test. Since we’re generating dynamic content based on the user, we need to log in during our test. Because we’re using Devise and Rails fixtures, we can use a convenient helper method, sign_in users(:one), to sign in a mock user.

require "application_system_test_case"
require "selenium-webdriver"

class DocsTest < ApplicationSystemTestCase
  include Devise::Test::IntegrationHelpers

  test "visiting the docs page" do
    user = users(:one)
    sign_in user
    visit '/'

    element = page.find_link(:text => "Contact Us")
    href = element['href']
    assert_includes href, "mailto:[email protected]"
    assert_includes href, "&subject="
    assert_includes href, user.email
    assert_includes href, user.id.to_s
  end
end

For dynamically-generated content, we want to confirm that the user-specific content makes it into the email draft with the subject field. Mailto links are very forgiving, in my experience. But if there’s a formatting error, you end up with just the “to” email address filled out.

Summing Up and Learning More

In this post, you’ve learned about mailto links—what to use them for and the basics of how to test them. If you’d like to learn more about mailto links, a great place to check out is the specification created by the Internet Engineering Task Force. That document covers the background of why mailto links exist. Also, it covers all the different parts of the specification in ways we don’t have time for here.

This short tutorial explored how to validate mailto links using standard browser tooling. But in doing so, we assumed that you don’t need to test the interaction between the browser and the email client. Your use case might require you to validate cross-application functionality. If you do, Testim has support for email validation, which enables you to verify the contents and metadata of emails sent to a Testim testing email address.

Until next time!

What to read next

Measuring & Communicating the Value of Test Automation: Community Voices

Regression Testing Examples: A Practical Walkthrough