Explain Codes LogoExplain Codes Logo

Can I set subject/content of email using mailto:?

html
mailto-link
email-encoding
uri-encoding
Alex KataevbyAlex Kataev·Aug 21, 2024
TLDR

To generate an email template using mailto: protocol, employ the syntax:

<a href="mailto:[email protected]?subject=Topic&amp;body=Content">Email me, Scotty!</a>

Clicking the link will autofill the email's subject line (subject=Topic) and body (body=Content). Use percent-encoding for spaces and special symbols (%20).

Handling extras within your email

Incorporating line breaks and special characters

To splice line breaks into the email body, use %0D%0A:

<!-- Who doesn't love a good vacation? --> <a href="mailto:[email protected]?subject=Vacation%20Plans&amp;body=Let's%20vacay%20on%3A%0D%0A10th%20July%202023">Send Request</a>

In JavaScript, you can let encodeURIComponent() do the heavy lifting and encode special characters:

// Escape the horrors of manual encoding const subject = encodeURIComponent('Special & symbolic / Inquiry'); const body = encodeURIComponent('Hello boss. \nI need a raise.'); let mailtoLink = `mailto:[email protected]?subject=${subject}&body=${body}`

Populating CC and BCC fields

To dispatch emails to multiple recipients, utilize the cc and bcc fields:

<a href="mailto:[email protected][email protected]&bcc[email protected]&subject=Report%20Update&amp;body=Inside: juicy gossips and a report">Email Update</a>

For complex mailto links, use tools like mailto.now.sh - it does the grunt work, so you don't have to.

Going beyond the basics

Encoding with PHP and HTML

In PHP, safeguard mailto: URI usage with htmlspecialchars and encode URL components with rawurlencode.

Consulting RFC standards

For an in-depth understanding and recommended practices, peek into RFC standards, particularly RFC 2368, RFC 1738, and RFC 3986.

Efficient email URIs

To ensure the delivery and readability of your emails, encode all special characters using percent-encoding.

Open-source mailto tool development

Consider using or collaborating on open-source mailto: tools for broader customization possibilities.