WordPress邮件嵌入HTML代码失效求助:HTML显示为纯文本
解决WordPress wp_mail发送HTML邮件显示纯文本的问题
1. 修复HTML实体转义问题
你代码里的HTML标签用了实体转义(比如<代替<),这会导致邮件客户端把这些当成纯文本显示,而非解析为HTML标签。把代码里的实体转义替换成真实的HTML标签:
// Get the Customer billing email $billing_email = $order->get_billing_email(); $subject = 'Your Email Subject'; // Message $message = ' <html><body><p>Here are the birthdays upcoming in August!</p></body></html> '; // To send HTML mail, the Content-type header must be set $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail( $billing_email, $subject, $message );
2. 使用过滤器强制设置HTML内容类型
wp_mail默认会重置邮件内容类型,仅通过headers参数设置可能被覆盖。添加wp_mail_content_type过滤器确保邮件以HTML格式发送:
// 定义设置HTML内容类型的函数 function set_html_content_type() { return 'text/html'; } // Get the Customer billing email $billing_email = $order->get_billing_email(); $subject = 'Your Email Subject'; // Message $message = ' <html><body><p>Here are the birthdays upcoming in August!</p></body></html> '; // 添加过滤器 add_filter( 'wp_mail_content_type', 'set_html_content_type' ); // 发送邮件 wp_mail( $billing_email, $subject, $message ); // 移除过滤器(避免影响后续邮件) remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
3. 排查插件/主题干扰
部分邮件类插件(如WP Mail SMTP)或自定义主题可能强制将邮件设为纯文本格式。暂时禁用所有第三方插件,切换到WordPress默认主题(如Twenty Twenty-Four)后重新测试。
4. 测试简化内容
先发送最基础的HTML内容(比如<p>Test HTML Email</p>),排除复杂HTML结构导致的解析问题。确认基础内容正常后,再逐步添加样式或复杂结构。
内容的提问来源于stack exchange,提问作者longest yard
相关产品推荐
相关产品推荐

