首页 > 解决方案 > wp_mail 不以 HTML 格式发送电子邮件

问题描述

我想从 wordpress 发送 html 电子邮件,我已经执行了以下操作以使其正常工作。它包括将内容设置为 html add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) ); 允许在 strip 标签中使用 html 标签(不管有没有它都不能工作)

<code>
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); 
</code>

其中包括所有标签,包括(逗号分隔的 ie '<div>,<link>..')。

我无法正确生成 html 电子邮件。在邮递员电子邮件日志中,我看到了 div、H3 等 html 部分,但样式 css 文件不存在(它已被 wp_mail 删除)。

$emailTo = 'abc@abc.com' ;
$subject = 'Updated about the process';
$headers = array('Content-Type: text/html; charset=UTF-8');
$headers []= 'From: xyz@xyz.com';
$body = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type"     
content="text/html charset=UTF-8" /></head><body>';
$body=$body.'<link rel="stylesheet" href="normal.css">';
$body=$body.$'<div class="flrow lar_overflow">';
$body=$body.'<div class="flexooa lar-line-header">Recent History</div>';
$body=$body.'<div class="flexiia lar-line-header"></div>';
$body=$body.'<div class="flexooa lar-line-header hlast"></div>';
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags());
wp_mail($emailTo, $subject, $body, $headers);

那么如何避免从生成的html内容中删除css文件标签。

标签: wordpress

解决方案


检查以下2个步骤:

  1. HTML 模板应该只有内联样式表。不要使用任何外部 CSS 路径。
  2. 默认情况下,WordPress 邮件被视为纯文本,因此使用 headers 参数或使用钩子将其更改为 HTML 类型。

检查此 URL:https ://developer.wordpress.org/reference/functions/wp_mail/

检查内容:默认内容类型是“文本/纯文本”,不允许使用 HTML。您可以使用“wp_mail_content_type”过滤器(参见下面的示例)或包含“Content-type: text/html”之类的标题来设置电子邮件的内容类型。但是,在发送消息后请小心将“wp_mail_content_type”重置为“text/plain”,因为不这样做可能会导致来自 WP 或插件/主题的电子邮件出现意外问题。

参考 URL 以通过钩子设置它:(检查以下 URL 的示例) https://developer.wordpress.org/reference/hooks/wp_mail_content_type/

它肯定会奏效!祝你好运。


推荐阅读