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

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

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

http://img2.mukewang.com/60f1557800010ad809880451.jpg

我还将提供 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";

}

?>


扬帆大鱼
浏览 277回答 2
2回答

慕慕森

我相信您需要设置 Mime 和 Content Type 才能发送包含 HTML 的消息。将以下内容添加到 $headers。$headers&nbsp;.=&nbsp;"MIME-Version:&nbsp;1.0\r\n"; $headers&nbsp;.=&nbsp;"Content-type:&nbsp;text/html\r\n";

慕斯709654

设置标题以支持 html 电子邮件模板。在标题中添加此代码。"Content-Type:&nbsp;text/html;&nbsp;charset=UTF-8"
打开App,查看更多内容
随时随地看视频慕课网APP