我想在博客文章下方创建一个“将此转发给 friend ”链接,打开我的电子邮件客户端,其中包含准备好的消息。我可以让它为单行消息工作,但我希望能够在消息正文中换行。我该怎么做?
<?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())
);
?>
如何开始“祝你有美好的一天!”在上面有空行的新行上?
The link: ...
Have a great day!
Stefaan
我的博客使用带有 Genesis 主题的 WordPress,我将我的代码放入启用了 PHP 的 genesis_footer_entry
Hook 中(参见 screenshot – 电子邮件文本为荷兰语)。
最佳答案
您不能在 mailto 的body 中使用 HTML 标记
根据 RFC 2368 ,这是不可能的:
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.
所以任何依赖于 HTML 标签的解决方案都是不可能的。 我建议的解决方案是使用 \r\n 以及 PHP 函数 rawurlencode
代码如下
<?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()))
);
?>
注意:我尝试将代码替换为携带变量 http://example.com/test.php 的 get_the_permalink()
引用资料:
What is the equivalent of JavaScript's encodeURIcomponent in PHP?
关于php - mailto 正文中的换行符 : link,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37524663/