Explain Codes LogoExplain Codes Logo

Mailto link multiple body lines

javascript
mailto
url-encoding
email-clients
Alex KataevbyAlex Kataev·Aug 14, 2024
TLDR

To construct a mailto link with newline breaks, use %0D%0A:

<a href="mailto:[email protected]?subject=Subject&body=First%20Line%0D%0ASecond%20Line">Email Us</a>

Copying the above snippet results in a clickable email link with preformatted body content.

URL encoding for new line

A URL doesn't naturally accommodate line breaks. To add line breaks to a mailto body, the URL encoding %0D%0A which represents CR/LF (carriage return/line feed) is used. This ensures compatibility across various email clients.

Special considerations

URLs have a specific set of characters that are formally recognized and it involves URL encoding to use special characters:

  • %0D%0A represents newline(CR/LF)
  • %0D is carriage return (CR)
  • %0A is line feed (LF)

In the URL, entire bodies should be encoded to hold the intended message formatting.

Compatibility with different email clients

Between various email clients, there exist discrepancies on how encoded characters are interpreted. It's always a good practice to test your mailto links across a wide range varied platforms to guarantee a consistent user experience.

URL encoding each character

Wanted to include special characters in your mailto link? Don't shoot yourself in the foot! They're to be URL encoded too. Spaces get %20 and & gets %26 to circumvent early termination of your body content.

Test across email clients

Older or non-standard email clients may interpret encoded characters differently. As they say, "the road to Outlook is paved with weird behaviors." Test your links on several clients to fine-tune the best experience.

Single 'body' parameter

One body parameter can include multiple lines. If more than one is used, Outlook may only consider the last 'body' parameter. Keep your email's body intact and in order with one parameter.

Implementing URI encodeURIComponent()

To achieve proper formatting, encodeURIComponent() is used in JavaScript for the entire email body, as it ensures that special characters and line breaks are correctly interpreted in the final link.

Compatibility using %0D%0A

For universal compatibility, encode your mailto links with %0D%0A. This double encoding is the standard for line breaks in email and works with all clients, no matter how much they like to do their own thing.