wp_mail 不发送 HTML 格式的电子邮件

问题描述 投票:0回答:2

我想从 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>

其中包含所有标签,包括(逗号分隔,即

'<div>,<link>..'
)。

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

$emailTo = '[email protected]' ;
$subject = 'Updated about the process';
$headers = array('Content-Type: text/html; charset=UTF-8');
$headers []= 'From: [email protected]';
$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个回答
0
投票

检查以下 2 个步骤:

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

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

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

通过钩子设置的参考网址:(查看下面网址的示例) https://developer.wordpress.org/reference/hooks/wp_mail_content_type/

一定会有效果的!祝你好运。


0
投票

HTML 模板应该只有内联样式表。

试试这个:

$body = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>';
$body .= '<style>
       .flrow { /* CSS */ }
       .lar_overflow { /* CSS */ }
       .flexooa { /* CSS */ }
       .lar-line-header { /* CSS */ }
       .hlast { /* CSS */ }
      </style>';
$body .= '<div class="flrow lar_overflow">';
$body .= '<div class="flexooa lar-line-header">Recent History</div>';
$body .= '<div class="flexiia lar-line-header"></div>';
$body .= '<div class="flexooa lar-line-header hlast"></div>';
$body .= '</body></html>';

更改标头以支持 HTML。

text/html

$headers = array('Content-Type: text/html; charset=UTF-8');
$headers[] = 'From: [email protected]';

$emailTo = '[email protected]';
$subject = 'Updated about the process';

wp_mail($emailTo, $subject, $body, $headers);
© www.soinside.com 2019 - 2024. All rights reserved.