最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - HTML email sent with wp_mail shows plain text

programmeradmin1浏览0评论

I have an HTML invoice that I want to send.

I use the following function to replace some things in the HTML

 foreach($variables as $key => $value){
  $template = str_replace('{{ '.$key.' }}', $value, $template);
}

After that I send the mail:

$to = get_option('admin_email');
$subject = "Someone reserved from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .    'Reply-To: ' . $email . "\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html';
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}

wp_mail($to, $subject, strip_tags($template), $headers);

The mail I get is like this:

Invoice article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary { display: block; } audio, canvas, video { display: inline-block; } audio:not([controls]) { display: none; height: 0; } [hidden], template { display: none; } html { font-family: sans-serif; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; } body { margin: 0; } a { background: transparent; } a:focus { outline: thin dotted; } a:active, a:hover { outline: 0; } h1 { margin: 0.67em 0; font-size: 2em ... 

(and the rest of the CSS), and then some HTML text without the HTML tags. So it does recognize HTML because it doesn't show <p> and other tags but it doesn't use the CSS.

I have an HTML invoice that I want to send.

I use the following function to replace some things in the HTML

 foreach($variables as $key => $value){
  $template = str_replace('{{ '.$key.' }}', $value, $template);
}

After that I send the mail:

$to = get_option('admin_email');
$subject = "Someone reserved from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .    'Reply-To: ' . $email . "\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html';
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}

wp_mail($to, $subject, strip_tags($template), $headers);

The mail I get is like this:

Invoice article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary { display: block; } audio, canvas, video { display: inline-block; } audio:not([controls]) { display: none; height: 0; } [hidden], template { display: none; } html { font-family: sans-serif; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; } body { margin: 0; } a { background: transparent; } a:focus { outline: thin dotted; } a:active, a:hover { outline: 0; } h1 { margin: 0.67em 0; font-size: 2em ... 

(and the rest of the CSS), and then some HTML text without the HTML tags. So it does recognize HTML because it doesn't show <p> and other tags but it doesn't use the CSS.

Share Improve this question edited May 17, 2019 at 0:03 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked May 13, 2014 at 17:17 MichaelAngeloMichaelAngelo 1011 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

You are passing the content of the message through the strip_tags() function. The result is the content of the email with the HTML tags striped. Try not using that function.

发布评论

评论列表(0)

  1. 暂无评论