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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵