Rawurlencode显示电子邮件中的ascii代码到正文和主题

时间:2013-12-22 15:30:57

标签: php wordpress email

制作此按钮,将通过访客电子邮件客户端在Wordpress中分享帖子的摘录和标题:

<a href="mailto:?subject=<?php echo rawurlencode()(get_the_title('','', false)) ?>&amp;body=<?php echo rawurlencode()(get_the_excerpt()) ?><?php the_permalink() ?>" title="Send a link to this post via email" rel="nofollow" target="_blank">Share this post via email</a>

99%的工作正常。唯一的问题是它显示了帖子的正文和标题中的ascii代码。 例如,我在电子邮件正文中看到“&hellip;”而不是[...]

如何解决这个小问题?

1 个答案:

答案 0 :(得分:2)

基本上,您可能需要解析get_the_titleget_the_excerpt通过

等调用返回的html实体
html_entity_decode(get_the_title())

因此,在您的情况下,您必须将rawurlencode的来电包裹在html_entity_decode附近:

<a href="mailto:?subject=<?php echo rawurlencode(html_entity_decode(get_the_title('','',   false)))?> ...

但请注意,根据您对html_entites_decode的使用情况,如果您的摘录包含引号或双引号,您的标记可能会搞砸并且无法正常工作,因此您需要对这些内容进行过滤。阅读文档了解更多信息:

http://www.php.net/html_entity_decode

(编辑:您可以将ENT_NOQUOTES参数传递给html_entity_decode,以便从转化中排除引号;但如果您的标题或摘录中包含引号,那么这些参数仍将以电子邮件的形式显示。)

此外,如果您的标题或摘录包含用户生成的内容,则在反转编码实体时可能会打开安全漏洞(例如,对于XSS注入)。所以我建议找另一种发送电子邮件的方式(例如某种推荐插件)。