方法一,用filter发送HTML邮件
发邮件用的函数是wp_mail(),wp_mail()则基于强大的邮件类phpMailer,所以发送HTML格式是小菜一碟。只是WordPress默认以纯文本格式发送邮件,我们收到的重设密码、评论提醒、用户注册等邮件都是纯文本格式的。
要发送HTML格式,wp_mail()给我们提供了一个filter可以改变content-type,在主题的functions.php或插件中写如下代码即可。
代码如下:
add_filter(‘wp_mail_content_type’,create_function(”, ‘return “text/html”;’));
代码如下:
add_filter(‘wp_mail_content_type’, ‘sola520_use_html’);
function sola520_use_html( $content_type ) {
if( ‘你的条件’ )
return ‘text/html’;
else
return $content_type;
}
方法二,用$headers实现
只要在header中指定content-type为text/html,wp_mail()就会用html格式发送邮件。
代码如下:
$headers = “MIME-Version: 1.0\n” . “Content-Type: text/html;”;
wp_mail(‘me@example.net’, ‘The subject’, ‘<p>The <em>HTML</em> message</p>’, $headers);
这样不用担心影响其它邮件,更省事了,适合发送自定义邮件。
搬瓦工中文网






