I decided to thoroughly understand this issue and consider all the attempts that were made here. If you want a quick answer , look at the currently accepted one.
Working solution
To use the text in the mailto: URL body parameter, percent-encode it. In PHP, use rawurlencode() . The object must also be encoded.
$subj = "Whatever subject"; $body = "Multi-\r\nline\r\nbody"; printf( "mailto:?subject=%s&body=%s", rawurlencode($subj), rawurlencode($body) );
mailto:?subject=Whatever%2Osubject&body=Multi-%0D%0Aline%0D%0Abody
To use it as the target of an HTML link , replace the characters that have special meaning in HTML with entity links - htmlspecialchars() used for PHP, and Wordpress has fancier (and filters) esc_html() .
$subj = "Whatever subject"; $body = "Multi-\r\nline\r\nbody"; printf( '<a href="%s">mailto: URL with multi-line body</a>', htmlspecialchars(sprintf( "mailto:?subject=%s&body=%s", rawurlencode($subj), rawurlencode($body) ), ENT_QUOTES) );
<a href="mailto:?subject=Whatever%2Osubject&Multi-%0D%0Aline%0D%0Abody">mailto: URL with multi-line body</a>
I am sure that by now you know how to change your PHP code to create such a link.
Debugging problems
You need to deal with several levels of technology , each of which has its own shielding scheme:
PHP (for example, "\n" is a line with a new line, not a backslash and the letter n; you will need to avoid the backslash if you want this to be "\\n" )
(s) printf (for example, to print the literal % , you need to write %% )
HTML (for example, & launches a link to an entity that should be taken literally, it should be encoded as a reference to the & object)
URL (for example, space is not a valid part of the URL and there must be percent encoding like %20 )
This is too difficult to do manually and to be sure that it is correct without checking the intermediate steps . If you made a mistake in any intermediate step, it is broken as a whole.
- Have you confirmed that the PHP code is saved exactly as you wrote it?
- Have you looked at the HTML that it creates?
- And you tried to copy the URL by right-clicking on the link in the browser and choosing Copy Email Address?
A safe approach is to manually create the product of the last step , and only after it works, continue and try to produce it programmatically. In this case, such a product is a URL that was once inserted into the browser location bar, opened by the mail composer with a pre-filled multiline body. You can start with a simple working example and make it more complex until it meets your needs. When the manual design is too tedious, you probably chose a product too complex - choose the simplest one that still works the way you want, you can add complexity later.
If you get stuck even when manually building the URL, you're out of luck. Either you need to learn more about the topic, seek help, or give up. If it's not just you, the problem lies somewhere between your browser and your email client. At least one of them has an error. In my experience with mailto: URLs, this is not uncommon. Even if you make money for you, visitors to your blog may be out of luck with their setup.
Additionally: HTML specification for submitted attempts
I took the time to see what the HTML specification says about the problems that I see in the attempts that were presented in the other answers and in the question itself. In particular:
- unencoded ampersand in attribute value
- New line in attribute value
- unencoded
< and > in HTML markup ( <br> ) in attribute value
Syntax level
Currently, HTML implementations are pretty reliable, and even the HTML specification has become very permissive regarding coding for ampersands ( & ) in entity references ( & is called symbolic links in HTML5), so you generally avoid this. (However, the HTML 4 specification required coding.)
At the actual byte level in the HTML file, the quoted attribute values in HTML5 can contain almost * any character other than the quote that is used to quote them (which should be replaced with a character reference).
* For more information, see Input stream preprocessing and the ambiguous ampersand in the HTML5 specification.
This is why even newlines and unencoded <br> are inside quotation marks. But in fact they are not in order in href (unlike, for example, title ), because ...
Semantic level
At the semantic level, when everything is parsed, any string (including an empty string) is allowed as an attribute value, but specific attributes add additional restrictions .
This is where HTML will bite you - href is a valid URL, potentially surrounded by spaces that cannot contain
- newlines and other space characters in the middle and
< , > and many other characters.
These characters must be percent encoding . For the actual definition of the URL and its syntax , the HTML specification refers to other specifications.
The processing of invalid URLs is determined by the implementation ; these mechanisms may not be compatible between browsers. Just follow the specifications. My Firefox ignores newlines and retains spaces - spaces are also not allowed in URLs, but Firefox encodes them before anything else with a URL.