A line break on the body of mailto: link

I want to create a “Forward this to a Friend” link below the blog post that my email client opens with a prepared post. I can make it work for a single line message, but I'd like to be able to break lines inside the message body. How can i do this?

<?php printf( '<a href="mailto:?subject=%s&body=%s">Forward this to a friend</a>', the_title_attribute('echo=0'), sprintf('The link: %s Have a great day! Stefaan', get_the_permalink()) ); ?> 

How do I get started "I have a great day!" on a new line with an empty line above?

Link:...

Have a great day!

Stefaan

My blog uses WordPress with a Genesis theme, and I put my code in the hook genesis_footer_entry with PHP enabled (see screenshot - Dutch email text.)

+6
source share
8 answers

You cannot use HTML tags in the mailto tag

According to RFC 2368 , this is not possible:

The special "body" hname indicates that the associated value of h is the message body. The "body" hname should contain the content for the first text / simple part of the message. The mailto URL is primarily intended to generate short text messages, which are actually the contents of automatic processing (for example, "subscribe" messages for mailing lists) rather than the general MIME bodies.

So any solution depends on HTML tags, this is not possible. The solution I propose is to use \ r \ n with the PHP rawurlencode function

So here is the code

 <?php printf( '<a class="simple-mail-link" href="mailto: x@y.com ?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>', 'My Subject', rawurlencode(sprintf( "Hi there, this might be interesting for you.\r\nThis is the link: %s.\r\nHave a great day!\r\nStefaan", get_the_permalink())) ); ?> 

Note. I tried the code with the replacement get_the_permalink () with a variable wrap http://example.com/test.php

links:

MailTo with HTML body

What is the equivalent of encodingURI JavaScript code in PHP?

+3
source

According to the email specification, each email line should end with an old <CR><LF> pair, so you need to use \r\n in the sprintf() .

"This is the link: %s.\r\n Have a great day!\r\n Stefaan"

And if \r\n does not work, you have a restriction on the source language. Given that:

  • \r - ASCII 13 (hex 0D )
  • \n is ASCII 10 (hex 0A )

maybe you could use "&#13;&#10;" ?

+2
source

Explanation

a mailto: is a URI scheme for an email address, like all other URIs, characters must be converted to a format that can be transmitted over the Internet.

Simplify your source code to illustrate the points below.

 printf(<href="mailto:?subject=%s&body=%s">Description</a>, the_title_attribute( 'echo=0' ) , sprintf("content link:%s more content", get_the_permalink()) 

The code works in the following sequences

  • get_the_permalink() gets the URL of your page
  • sprintf() replaces %s in $content result of step 1
  • print_f() then replaces i) the first %s ie subject=%s with the result of the_title_attribute( 'echo=0' ) retrieved, which I assume is the name of the page or your site, and then ii) the second %s ie body=%s with the result of step 2 to form a complete link a mailto

To save line breaks, you must convert the $content string to an encoded URL form before exiting.

In PHP, which is executed using the rawurlencode() function, it translates line breaks \r\n into a string in %0D%0A , according to RFC 3986 .

Since the percent sign % used to specify conversion to sprint_f() , you must pass the string to rawurlencode() after the string has been formatted by sprint_f() , so use rawurlencode(sprint_f($string, $arg))


Decision
I have fully assembled a solution for copying and pasting into your widget to avoid false negative result due to incorrect implementation.

Format 1 and format 2 contain essentially the same code, two formats to illustrate how you can insert a line break, either 1) pressing enter OR 2) or typing in \r\n where line break is required .

I made $content separate line to simplify the editing and readability of the code.

Format 1

  <?php //Start new line by pressing enter //IMPORTANT NOTE: do not remove the %s $content="Hi there, this might be interesting for you This is the link: %s Have a great day! Stefaan "; //No need to amend the code below printf( '<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"> <div class="label"> <div class="dashicons dashicons-admin-plugins"></div> Forward this to a friend</div></a>',the_title_attribute( 'echo=0' ), rawurlencode(sprintf( $content,get_the_permalink())) ); ?> 

Format 2

 <?php //Start new line by adding \r\n //IMPORTANT NOTE: do not remove the %s $content="Hi there, this might be interesting for you\r\nThis is the link: %s\r\nHave a great day!\r\nStefaan"; //No need to amend the code below printf( '<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"> <div class="label"> <div class="dashicons dashicons-admin-plugins"></div> Forward this to a friend</div></a>',the_title_attribute( 'echo=0' ), rawurlencode(sprintf( $content,get_the_permalink())) ); ?> 

Demo

 <a class="simple-mail-link" href="mailto:?subject=Page%20Title&amp;body=Hi%20there%2C%20this%20might%20be%20interesting%20for%20you%0D%0AThis%20is%20the%20link%3A%20http%3A%2F%2Fblahblahblah.com%0D%0AHave%20a%20great%20day%21%0D%0AStefaan"><div class="label"> <div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a></body> 
+2
source

Use urlencoded string for CR / LF

 %0D%0A 

where do you want a break.

In the example

 <a class="simple-mail-link" href="mailto:?subject=hello&body=Hello%0D%0A%0D%0AWorld!" > 
0
source

Add another %s argument to your sprintf() call and pass it the root channel version of char %0A (twice) URL encoding:

 printf( '<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>', 'subject', sprintf( 'Hi there, this might be interesting for you. This is the link: %s. %sHave a great day! Stefaan', "link-here", "%0A%0A" ) ); 

Edit: Alternatively, you can also simply add %0A to the line itself, but you must remember to avoid % char by adding an additional file in front of it:

 printf( '<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>', 'subject', sprintf( 'Hi there, this might be interesting for you. This is the link: %s. %%0A%%0AHave a great day! Stefaan', "link-here" ) ); 
0
source

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&amp;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 &amp; 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 ( &amp; 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.

0
source

You can add the <br> tag to your sprintf (provided that you want to display an interrupt string for HTML):

 sprintf( 'Hi there, this might be interesting for you. This is the link: %s.<br> Have a great day!<br> Stefaan',get_the_permalink() ) 

EDIT:

If you want to use plain text mail \r\n and double quotes:

 sprintf("Hi there, this might be interesting for you. This is the link: %s.\r\n Have a great day!\r\n Stefaan",get_the_permalink() ) 
-one
source

You could probably add PHP_EOL there.

 <?php printf( '<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>', the_title_attribute( 'echo=0' ), sprintf( 'Hi there, this might be interesting for you. This is the link: %s. %sHave a great day! %sStefaan',get_the_permalink(), PHP_EOL, PHP_EOL ) ); 
-one
source

All Articles