dongsha1544 2015-05-22 17:38
浏览 82
已采纳

以HTML格式发送电子邮件[重复]

This question already has an answer here:

I am setting up a confirmation email script for our customers to receive an email when they complete the online form. I uploaded my script and tested but I received an email with just the raw HTML. I have been looking at some tutorials trying to do this as well. I validated my HTML and I passed all the tests. Is there something that I have to include in my PHP for my body string to be interpreted as HTML?

Here is my script

<?php
//Script for sending a confirmation email
$body = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
 <head>
  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
  <title>Demystifying Email Design</title>
  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
</head>
<body style=\"margin: 0; padding: 0; background-color: #ecf0f1;\">
 <table cellpadding=\"0\" cellspacing=\"0\" width=\"60%\" align=\"center\" style=\"border: 1px solid #bdc3c7;\">
  <tr bgcolor=\"#3498db\">
   <td align=\"center\">
    <img alt=\"Logo\" src=\"http://www.****.com/logo-lg.jpg\" style=\"width: 50%; height: auto; padding-top: 5%; padding-bottom: 5%;\" />
   </td>
  </tr>
  <tr bgcolor=\"#ffffff\" align=\"center\">
   <td>
    <h3 style=\"padding-top: 5%; padding-bottom: 5%;\">Worldwide innovator in flexible liquid packaging</h3>
   </td>
  </tr>
  <tr align=\"center\" bgcolor=\"#ffffff\">
   <td>
    <h4 style=\"padding-top: 5%; padding-bottom: 5%;\">Thank you for contacting customer service. We've received your sample request; one of our team members will be in contact with you in the very near future. Thanks again for your time and interest.</h4>
   </td>
  </tr>
 </table>
</body>
</html>";

$to = "*******@****.com";
$subject = "Thanks for Reaching out";

if (mail($to,$subject,$body)){
    echo "Mail was sent";
} else{
    echo "Failed";
}

This is the email I received

This is the email I got

</div>
  • 写回答

2条回答 默认 最新

  • douyuanliao8815 2015-05-22 17:41
    关注

    You need to also set a header indicating this is HTML, e.g.

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "
    ";
    $headers .= "Content-type:text/html;charset=UTF-8" . "
    ";
    
    // More headers
    $headers .= 'From: <webmaster@example.com>' . "
    ";
    $headers .= 'Cc: myboss@example.com' . "
    ";
    mail($to, $subject, $message, $headers);
    

    Source (see Example 3).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog