7.8. Putting Additional Information in mailto Links

Problem

You need to pre-format an email message generated from a link on your web site.

Solution

Extend the mailto link to include values for the message subject, body, and additional recipients.

A basic mailto link on a web page creates a new email message in the user's default email application. It's a good idea to make the actual address the link, because that helps reinforce for the user what happens when she selects the link:

	<a href="mailto:address@domain.com">address@domain.com</a>

You can create more complex links that fill in parts of the message. With Subject and Body, you can ensure at least some meaningful content in the email messages you receive from your site:

	<a href="mailto:address@domain.com?Subject=
	   web site%20comment&Body=Type%20your%20message%20here:">
	   address@domain.com</a>

Tip

The characters %20 indicate a space in the encoded URL, though most browsers can handle an unencoded space.

You also can add additional recipients in the To:, Cc:, and Bcc: headers of the message by separating their addresses with commas:

	<a href="mailto:address@domain.com,another@domain.com&
	   Cc:afriend@domain.com&Bcc:someone@domain.com">address@domain.com</a>

Discussion

Unprotected email addresses in web page code are like blood in the water for spam sharks. If you must use linked email addresses on your site (rather than an email form that hides actual addresses), be sure to read Recipe 9.11 for more information. Most of the protection techniques ...

Get Web Site Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.