Php 使用特殊字符在Wordpress中发送邮件
我已经制作了一个插件,每次在Wordpress中发布新帖子时,它都会向管理员发送一封电子邮件(最终将发送给订阅者)。在我的插件中,我有一个下表格式的电子邮件模板Php 使用特殊字符在Wordpress中发送邮件,php,wordpress,email,post,special-characters,Php,Wordpress,Email,Post,Special Characters,我已经制作了一个插件,每次在Wordpress中发布新帖子时,它都会向管理员发送一封电子邮件(最终将发送给订阅者)。在我的插件中,我有一个下表格式的电子邮件模板 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> &l
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Email test</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td width="20" valign="top"></td>
<td width="560" valign="top">%%CONTENT%%</td>
<td width="20" valign="top"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
电子邮件测试
%%含量%%
我用帖子中的内容替换%%CONTENT%%。我尝试过通过各种函数和过滤器运行内容,包括:apply_filters、htmlspecialchars()和htmlentities(),但没有成功。当我在Outlook中收到电子邮件时,里面有奇怪的字符。我的帖子包含一些特殊的字符,如、&、、'和学位符号°C
这是插件代码:
function post_published_notification( $ID, $post ) {
global $post;
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$content = $post->post_content;
$content = apply_filters('the_content', $content);
$html_content = file_get_contents(__DIR__.'/email_components/email.html');
$message = str_replace('%%CONTENT%%', $content, $html_content);
$headers[] = 'MIME-Version: 1.0' . "\r\n";
$headers[] = 'Content-type: text/html; charset="UTF-8' . "\r\n";
wp_mail( $to, $subject, $message, $headers );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
函数发布发布通知($ID,$post){
全球$员额;
$author=$post->post_author;/*post author ID*/
$name=获取作者元('display\u name',$author);
$email=获取作者元('user\u email',$author);
$title=$post->post\u title;
$to[]=sprintf('%s',$name,$email);
$subject=sprintf('Published:%s',$title);
$content=$post->post\u内容;
$content=apply_过滤器('the_content',$content);
$html_content=file_get_contents(uuu DIR_uuu.'/email_components/email.html');
$message=str_replace('%%CONTENT%%',$CONTENT,$html_CONTENT);
$headers[]=“MIME版本:1.0”。“\r\n”;
$headers[]='内容类型:text/html;charset=“UTF-8”。“\r\n”;
wp_邮件($to、$subject、$message、$headers);
}
添加行动('publish_post'、'post_published_notification',10,2);
因此,我的问题是如何使这些字符在电子邮件主题和电子邮件正文中正确显示?非常感谢!Mike在尝试了许多不同的选项后,我通过使用wordpress循环输出电子邮件内容解决了这一问题:
$thepost = new WP_Query('post_type=news&p='.$post_id);
if($thepost->have_posts()) {
while($thepost->have_posts()) {
//runs once to get the one queried post
$thepost->the_post();
$content = wpautop(get_the_content());
}
}