wordpress - 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文件标签。
解决方案
检查以下2个步骤:
- HTML 模板应该只有内联样式表。不要使用任何外部 CSS 路径。
- 默认情况下,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/
它肯定会奏效!祝你好运。
推荐阅读
- r - 根据 R 中的两个列 ID 选择的因子水平对数据进行子集化
- deep-learning - 两层网络上的反向传播
- google-chrome - 删除存储在“使用 Google Smarts 进行 Web 浏览”下的用户
- reactivesearch - 来自 Reactivesearch 的 DataSearch 左对齐自动完成建议
- javascript - onClick 仅适用于其他所有人
- java - 如何使 CardLayout 与任意数量的卡片一起使用?
- c# - C#,是 Graphics.DrawImage 复制输入位图吗?
- solr - 从 SOLR 索引中删除 null
- .net - 在 iframe 中验证 Web 应用程序
- python - 如何访问/拆分包含列表的列中的项目