1. Computing

Discuss in my forum

Elements of a Mailto: URL

You can specify default Subjects, body text and more in email links

By

Updated April 17, 2014
If you enable your web site visitors to send you email via a mailto: link, you have provided them with a particularly convenient way of establishing contact. All they have to do is click and start typing their message.

But this convenience is not all the mighty mailto: has in store. Let's say you want to define a default Subject, for example, so you know the email was sent via the mailto: link on your web site. Or maybe you want to suggest a default text for the body if visitors click on a particular link (you can use this to create a simple survey, for example).

Let's find out how these advanced but by no means difficult uses of mailto: work.

The Mailto: URL

A mailto URL basically consists of three parts. First comes

  • the word mailto: followed by
  • the recipient's email address, and eventually come
  • "header" lines defining default values for various message parts.

To

The recipient's email address (that following the mailto: immediately) may actually be more than one address. Multiple addresses are separated by comma (just as you would use it in the To: line of your email client). Interestingly, a mailto: link is also valid (and works) if it contains no default recipient address at all.

It is therefore perfectly legal to put the default subject (using ?subject=) directly after mailto:. We can create a message with a default subject but no default recipient: mailto:?subject=Doc,%20do%20da%20dance.

Headers

The best things of course come last. In the "header" part of the mailto: URL we can do almost anything. Any header name and value specified in RFC 2822 — The Internet Message Format — can be used in theory.

Not only can we specify "Subject:" lines, but also "Cc:" (sending a carbon copy) or "Bcc:" (a blind carbon copy).

X-Headers

Of particular interest is the ability to "invent" arbitrary header lines. They only need to be preceded by "X-" to be legal -- maybe you know the ubiquitous "X-Mailer:" header. Together with an email client capable of filtering based on arbitrary header lines this makes for wonderful sorting and filtering possibilities.

All header lines are specified in the same way we already know from the subject: [header name]=[header value], for example X-Z=Y.

Unfortunately, such X-Headers work with hardly any combination of browser and email client, so you cannot rely on them to be sent.

Default Message Text

Finally, there is one special header name: body.

With this "header", you can specify text that will appear in the email message's body. The body part of the mailto: URL scheme is intended primarily for short textual messages.

Due to the way links work in HTML, special characters must be encoded when you construct a default body for a mailto: link. A space must be translated to "%20", for example, and a line break becomes "%0D%0A".

Fortunately, you do not have to remember these encodings or do them by hand. Use JavaScript or the convenient mailto: URL encoder instead.

Combining Headers

But how can you have more than one "header" part, a default Subject and a default message text, for example? This is done with an ampersand: &.

The first header follows the recipient's address after a question mark: "?". All future headers are attached in any order, separated by ampersands.

For example (the line breaks are here only to ensure neat looks; of course you would not insert them into the URL):
mailto:recipient@example.com
?subject=Ha!
&X-Mailer=Baluba
&body=Ha!%0D%0ABla!

Related Video
Create Email Aliases and Forwarding Addresses in Gmail

©2014 About.com. All rights reserved.