Crafting Email Links with Meaningful Content
Email links, created using the mailto:
scheme in HTML, are a convenient way to initiate an email message for your users. However, controlling the content of that pre-populated email – particularly formatting – can be trickier than it appears. This tutorial will guide you through the capabilities and limitations of mailto:
links, covering how to include subjects and bodies, and how to work around formatting constraints.
The Basics of mailto:
Links
The fundamental structure of a mailto:
link is straightforward:
<a href="mailto:[email protected]">Email Us</a>
This creates a link that, when clicked, will open the user’s default email client and pre-populate the "To:" field with "[email protected]".
Adding a Subject Line
You can also pre-populate the subject line by appending ?subject=
followed by the desired subject to the mailto:
link:
<a href="mailto:[email protected]?subject=Inquiry about your services">Email Us</a>
Pre-Populating the Email Body
Adding a body to the email is similar. Append &body=
to the link, followed by the text you want to include.
<a href="mailto:[email protected]?subject=Inquiry&body=I am interested in learning more about your products.">Email Us</a>
The Limitations of HTML Formatting
While it’s tempting to include HTML tags within the body
parameter to format the email (e.g., <b>bold text</b>
), this is generally not supported by most email clients. The mailto:
scheme is designed for simple text content. Email clients will often interpret HTML tags as literal text, displaying them within the email body instead of rendering them as formatting. RFC 6068 specifically outlines this limitation.
Working Around Formatting Constraints
So, how can you improve the readability and structure of your pre-populated email bodies? Here are a few options:
1. Plain Text Formatting:
Use whitespace and line breaks to create visual structure. While you can’t use HTML, you can still make the email easier to read.
<a href="mailto:[email protected]?subject=Order Inquiry&body=Dear Support,\n\nI am writing to inquire about my recent order.\n\nOrder Number: 12345\n\nThank you,\nJohn Doe">Email Support</a>
2. URL Encoding Special Characters:
Certain characters, like line breaks and carriage returns, need to be URL-encoded to be correctly interpreted within the mailto:
link.
- Line Break: Use
%0D%0A
or%0A
(the latter is more widely supported) - Carriage Return:
%0D
Here’s an example using URL encoding for line breaks:
<a href="mailto:[email protected]?subject=Feedback&body=Thank%20you%20for%20your%20feedback.%0D%0AWe%20appreciate%20your%20business.">Submit Feedback</a>
3. Advanced Approaches: Creating EML Files (For Rich Text)
If you absolutely require rich text formatting (e.g., bold, italics, tables), the mailto:
scheme isn’t the right solution. A more complex approach involves generating an EML (email message) file on the client-side using JavaScript and prompting the user to download it. This allows you to include fully formatted HTML within the email content. This is beyond the scope of a simple tutorial, but several resources detail how to implement this technique.
Best Practices
- Keep it Concise: Pre-populate the email with essential information, but avoid lengthy messages.
- Provide Clear Instructions: If you’re pre-populating the subject and body, let the user know what’s happening. For example, "Subject: Order Inquiry (Pre-populated)".
- Prioritize Readability: Use whitespace and line breaks to improve the visual structure of the pre-populated content.
- Test Thoroughly: Test your
mailto:
links in different email clients and browsers to ensure they function as expected. - Consider Accessibility: Ensure the pre-populated content is accessible to users with disabilities.