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).

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dptgpyl61857413 2015-05-22 17:46

    Correct code will be:

    $to = "*******@****.com";
    $subject = "Thanks for Reaching out";
    
    $headers = "MIME-Version: 1.0" . "
    ";
    $headers .= "Content-type:text/html;charset=UTF-8" . "
    ";
    
    if (mail($to,$subject,$body,$headers)){
       echo "Mail was sent";
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题