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?
<?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 start “Have a great day!” on a new line with an empty line above?
The link: ...
Have a great day!
Stefaan
My blog uses WordPress with a Genesis theme and I put my code into the genesis_footer_entry
hook with PHP enabled (see screenshot – email text is in Dutch).
Explanation
a mailto:
is a URI scheme for email address, like all other URIs, characters need to be translated into a format that can be transmitted over the Internet.
Take a simplified form of your original 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 runs in the following sequences
get_the_permalink()
gets the URL of your page sprintf()
replaces %s
in $content
with the result of step 1print_f()
then replaces%s
i.e. subject=%s
with the fetched result of the_title_attribute( 'echo=0' )
, which I assume
is the title of the page or your site and then
ii) the second %s
i.e. body=%s
with the result of step 2 to form a complete a mailto
linkIn order to retain the line breaks, you should convert the $content
string into URL encoded form before outputting it.
In PHP that's done by using the rawurlencode()
function, it translates line breaks \r\n
in string into %0D%0A
, as per RFC 3986.
Since the percentage sign %
is used for conversion specification in sprint_f()
, you should pass the string to rawurlencode()
after the string has been formatted by sprint_f()
, therefore the use of rawurlencode(sprint_f($string, $arg))
Solution
I have put together the solution in full for you to copy and paste into your widget to avoid false negative due to wrongful implementation.
Format 1 and format 2 contain essentially the same code, the two formats are there to illustrate to you how you can insert line break, either by 1) pressing enter
OR 2) by typing in \r\n
where line break is needed.
I have made $content
a separate string for your ease of editing and readability of 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&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>
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 protected]?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
references:
MailTo with HTML body
What is the equivalent of JavaScript's encodeURIcomponent in PHP?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With