HTML电子邮件显示为代码而不是CSS页面

时间:2019-05-28 18:39:43

标签: php html css email

我想制作一个自动的html / css电子邮件,我在其中使用了mail()函数,但它显示为代码

我已经在标题中提供了文档类型和内容,但仍显示为代码:

enter image description here

我还把提供doctype的头连接到了头中,但是它仍然不起作用。

这是我的代码:


<?php


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
$email_from = "info@domain.com";
    $email_message = "

<html xmlns='http://www.w3.org/1999/xhtml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:v='urn:schemas-microsoft-com:vml' style='line-height: inherit;'>
<head>

<meta content='text/html; charset=utf-8' http-equiv='Content-Type'>
<meta content='width=device-width' name='viewport'>

<meta content='IE=edge' http-equiv='X-UA-Compatible'>

<title></title>


<style id='media-query' type='text/css'>
@media only screen and (min-width: 670px) {
  .block-grid {
    width: 650px !important;
  }


  .desktop_hide {
    display: block !important;
    max-height: none !important;
  }
}
</style>
</head>
<body class='clean-body' style='line-height: inherit; margin: 0; padding: 0; -webkit-text-size-adjust: 100%; background-color: #FFFFFF;'>
<style id='media-query-bodytag' type='text/css'>

</tr>
</tbody>
</table>
</
</table>
</body>
</html>";


$to = "email@gmail.com";
$subject = "Enquiry";
$txt = $email_message;
$headers1="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional //EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
$headers2 = "From:". $email_from . "\r\n" .
"CC: email@gmail.com";
$headers=$headers1.$headers2;

if (mail($to,$subject,$txt,$headers))
{
    //echo "Thank You for contacting us, we will get back to you soon";
}
?>

2 个答案:

答案 0 :(得分:1)

我相信您需要设置Mime和Content Type才能发送包含HTML的消息。将以下内容添加到$ he​​aders。

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";

答案 1 :(得分:0)

设置标题以支持html电子邮件模板。将此代码添加到标题中。

"Content-Type: text/html; charset=UTF-8"