I want to create a “Forward this to a friend” link below a blog post that opens my email client with a prepared message. I can get it to work for a single-line message, but I’d like to be able to break lines inside the message body. How can I do that?
'<a href="mailto:?subject=%s&body=%s">Forward this to a friend</a>',
sprintf('The link: %s Have a great day! Stefaan', get_the_permalink())
The link: ...
Have a great day!
You cannot use HTML tags in body of mailto
According to RFC 2368, this is not possible:
The special hname "body" indicates that the associated hvalue is the body of the message. The "body" hname should contain the content for the first text/plain body part of the message. The mailto URL is primarily intended for generation of short text messages that are actually the content of automatic processing (such as "subscribe" messages for mailing lists), not general MIME bodies.
So any solution depends on HTML tags is not possible. The solution I'm suggesting is to use \r\n along with PHP function rawurlencode
So here's the code
<?php printf( '<a class="simple-mail-link" href="mailto:email@example.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 replacing get_the_permalink() with variable carrying http://example.com/test.php