以HTML格式发送电子邮件

时间:2015-05-22 17:38:55

标签: php html email outlook

我正在设置确认电子邮件脚本,以便我们的客户在完成在线表单时收到电子邮件。我上传了我的脚本并进行了测试,但收到了一封只包含原始HTML的电子邮件。我一直在寻找一些试图做到这一点的教程。我验证了我的HTML,并通过了所有测试。我必须在PHP中包含一些内容,以便将我的正文字符串解释为HTML吗?

这是我的剧本

<?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 got

2 个答案:

答案 0 :(得分:2)

您还需要设置一个标题,表明这是H​​TML,例如

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to, $subject, $message, $headers);

Source(参见示例3)。

答案 1 :(得分:1)

正确的代码将是:

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

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

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