php - mailto 正文中的换行符 : link

标签 php email line-breaks mailto email-client

我想在博客文章下方创建一个“将此转发给 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 – 电子邮件文本为荷兰语)。

最佳答案

您不能在 mailtobody 中使用 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.phpget_the_permalink()

引用资料:

MailTo with HTML body

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

关于php - mailto 正文中的换行符 : link,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37524663/

相关文章:

php - 数据库清理检查所有者

php - 如何通过为空字段添加 NULLS 将 CSV 文件导入 MySQL?

email - 在 Dart 中发送 SMTP 电子邮件

linux - 设置 linux 服务器以使用 Exchange 服务器作为中继

php - CodeIgniter session 中的 user_data 用来做什么?

java - 为 Android webview 隔离网站 DIV

php - Laravel - 将数据传递到电子邮件 View

python - 如何防止 vim 破坏字符串文字?

ios - UILabel不显示用户输入文本

javascript - 如何从文本区域获取换行符