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

标签 php html css email

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

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

enter image description here

我还将提供文档类型的 header 连接到 header 中,但它仍然不起作用。

这是我的代码:


<?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";
}
?>

最佳答案

我相信您需要设置 Mime 和内容类型才能发送包含 HTML 的消息。将以下内容添加到 $headers。

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

关于php - HTML 电子邮件显示为代码而不是 css 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56348292/

相关文章:

php - 如何访问ArrayCollection数据

jquery - 类型错误 : variable is undefined; i get this error in specific pages

css - 在手机/平板电脑上保持定位(响应式)

html - Bootstrap : A blank column which isn't in code, 但它在网格中

iphone - 打开 native 应用程序。来自 Safari

css - 当 * 定义为边框 :0 时添加表格边框

php - 搜索字谜词的最快方法是什么?

php - 为什么执行 PHP mysql_query 失败

php - 字段不是实体的有效字段 - DoctrineEncryptBundle Symfony

javascript - 用 X 替换 html 复选框