发送电子邮件时WordPress Get_the_Content将丢失格式

时间:2016-05-11 作者:Yazmin

我将一篇帖子的内容拉到循环之外,以发送一封电子邮件,如下所示:

$content = get_the_content();
然而,当我构思电子邮件正文时:

$body =  \'Hello:

<br/><br/>

\'.get_the_title().\' was added to PGOPs Notes. You can view it here:
<br/><br/>

<a href=\'.get_permalink($post->ID).\'>\'.get_permalink($post->ID).\'</a>
<br/><br/>

\'.$pgops_fr_format_assigned_label.\'
\'.$pgops_fr_trt_label.\'
\'.$pgops_new_structure_assigned_label.\'

\'.$content.\' 
<br/><br/>

Thank You, <br/>
\'.$first_name.\' \'.$last_name.\' <br/>
Programming Operations\';
并通过wp\\u mail()发送:

wp_mail( $emails, \'New PGOPs Note: \'.$title.\'\', $body, $headers );
我丢失了电子邮件中内容变量的格式。$content变量的输出示例应为:

2016年8月改版

(2016年4月28日)AH-基于更新TRT的新格式

最大2.5 DSCLM/12B/13S/99:55

但我在邮件中得到的是:

2016年8月(2016年4月28日)AH的重新格式-基于更新的TRT2的新格式。最大5 DSCLM/12B/13S/99:55

如何保持电子邮件中内容的格式?

谢谢

1 个回复
最合适的回答,由SO网友:N00b 整理而成

默认电子邮件内容类型为text/plain 不允许使用HTML.

将此添加到functions.php 文件:

// use HTML instead of plain text
add_filter( \'wp_mail_content_type\', \'my_awesome_mail_content_type\' );

function my_awesome_mail_content_type() {

    return \'text/html\';
}
但请注意,不同的电子邮件客户端对CSS 规则。。Read more from here.

<小时>Alternative: 如果你想继续使用text/plain, 您需要避免任何HTML (它可能会弄坏东西)并使用\\n 而不是<br/> - 基本上是一样的,只是text/plain 版本