Stefaan Stefaan - 4 months ago 18x
PHP Question

Line break in body of mailto: link

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())

How do I start “Have a great day!” on a new line with an empty line above?

The link: ...

Have a great day!


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


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


    '<a class="simple-mail-link" href=""><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


MailTo with HTML body

What is the equivalent of JavaScript's encodeURIcomponent in PHP?