How to Build a mailto Link with Subject and Body

Build a mailto link with subject and body using correct percent-encoding, line breaks, accessible link text, and a visible email fallback.

Published
Reading time
5 min read
How to Build a mailto Link with Subject and Body
On this page

A mailto: link can open a visitor’s email app with a recipient, subject, and message already filled in. The tricky part is encoding: spaces, line breaks, ampersands, and non-ASCII characters must be represented correctly so they remain part of the intended value.

Key Takeaways

  • A basic link uses mailto:address?subject=value&body=value.
  • Percent-encode each subject and body value separately. Use %20 for spaces and %0D%0A for line breaks.
  • Encode an ampersand inside a value as %26; keep the actual parameter separator as &.
  • A mailto link depends on the visitor’s device and email-client configuration. Provide a visible email address as a fallback.

The recipient goes after mailto:. Optional fields such as subject and body follow a question mark and are separated from each other with ampersands:

mailto:hello@example.com?subject=Question%20about%20pricing&body=Hello%2C%20I%20have%20a%20question.

Here, the first & separates two fields. If a value itself contains an ampersand, encode it as %26 so it is not mistaken for a separator. Encode values individually, rather than encoding the whole URI; otherwise, the structural characters that make the link work may be encoded too.

Advertisement

Encode spaces, line breaks, and special characters

Use percent-encoding for characters in the subject and body. Spaces should be %20, not +: plus signs can be interpreted literally in a mailto value. Use CRLF (\r\n) line endings in the body, represented in the link as %0D%0A.

For example, this subject contains an ampersand, and the body has a blank line and non-ASCII text:

mailto:hello@example.com?subject=Question%20about%20pricing%20%26%20plans&body=Hello%2C%0D%0A%0D%0AHola%2C%20%C2%BFpueden%20ayudarme%3F

The ¿ character is encoded as UTF-8 bytes (%C2%BF) and then percent-encoded. Encoding text as UTF-8 before percent-encoding lets the link represent non-ASCII characters reliably. Email clients may still display or handle prefilled text differently.

This helper encodes the subject and body separately and normalizes body line endings to CRLF. The recipient is a fixed, trusted address in this example.

function encodeMailtoValue(value) {
  return encodeURIComponent(value).replace(/[!'()*]/g, (character) =>
    `%${character.charCodeAt(0).toString(16).toUpperCase()}`
  );
}

function makeMailtoHref(subject, body) {
  const normalizedBody = body.replace(/\r\n|\r|\n/g, "\r\n");
  return (
    "mailto:hello@example.com" +
    `?subject=${encodeMailtoValue(subject)}` +
    `&body=${encodeMailtoValue(normalizedBody)}`
  );
}

const href = makeMailtoHref(
  "Question about pricing & plans",
  "Hello,\n\nI’m interested in your plans."
);

console.log(href);

encodeURIComponent handles spaces, ampersands, and UTF-8 characters in each value. The extra replacement percent-encodes a few punctuation characters it normally leaves unescaped. Do not pass the whole mailto: URI through this function: doing so would encode its separators as well.

When writing HTML, write parameter separators as & in the attribute. The browser interprets that HTML entity as & in the URI.

<a href="mailto:hello@example.com?subject=Question%20about%20pricing%20%26%20plans&amp;body=Hello%2C%0D%0AI%20have%20a%20question.">
  Email us about a question
</a>
<p>You can also email us at <a href="mailto:hello@example.com">hello@example.com</a>.</p>

The second link makes the email address visible as a fallback. It also gives visitors a way to copy it if their device does not have a mail handler configured.

  1. Decode the subject and body separately to confirm that punctuation and non-ASCII text survived encoding.
  2. Check that body line breaks use %0D%0A and that an ampersand inside a value is %26.
  3. Open the page on a device with an email handler configured and check that the recipient, subject, and body appear as intended.
  4. Test more than one relevant client if your audience uses different devices or email apps. I did not test these examples in live email clients, so client-specific behavior is not verified here.

A mailto link asks the visitor’s system to open an email handler; it does not send a message automatically. Clients may ignore fields, alter formatting, or handle long prefilled messages differently. Keep important instructions on the page rather than only in the prefilled body.

Advertisement

Common Mistakes

  • Using + for spaces: Use %20 in mailto values; a plus may appear as a plus rather than a space.
  • Leaving a value’s ampersand unencoded: Encode it as %26 to avoid accidentally starting another field.
  • Encoding the entire URI: Encode the subject and body values, not the mailto: scheme, recipient, or field separators.
  • Using only a vague link label: “Click here” does not explain what the link does. Use descriptive text and show the email address as a fallback.
  • Assuming every client behaves alike: A prefilled message is a convenience, not a guarantee. Check the link in the environments that matter to your audience.

Best Practices

  • Keep the recipient address fixed where possible. If you build it from user-controlled input, validate that input and prevent it from introducing unexpected recipients or URI fields.
  • Keep prefilled messages short and optional. Visitors should be able to edit them before sending.
  • Use a descriptive link label, such as “Email our support team,” and provide the address visibly nearby.
  • Treat prefilled content as untrusted input when you use it in a web application. Encode values before placing them in a URI or HTML attribute, and use the appropriate escaping for the output context.

Conclusion

A correct mailto link keeps its structure separate from its encoded values: use %20 for spaces, %0D%0A for body line breaks, and %26 for an ampersand inside a value. Pair the link with descriptive text and a visible email address, then check it in the email clients and devices your visitors use.

References

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.