HTML电子邮件只显示消息中的html

时间:2013-07-22 22:00:42

标签: php html email

我正在尝试使用php中的简单mail()函数发送邮件,并在HTML中完成格式化

PHP代码:

$headers = "From: ClubbedIn\r\n";
$to = $userEmail;
$subject = $rowclub['clubName']." - New Event: ".$eventName;
$message = '<html><body>';
$message .= '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';    
$message .= "<tr><td><strong>Event Name:</strong> </td><td>" . strip_tags($eventName) . "</td></tr>";
$message .= "<tr><td><strong>Date:</strong> </td><td>" . strip_tags($newdate) . "</td></tr>";
$message .= "<tr><td><strong>Time:</strong> </td><td>" . $startTime2." - ".$endTime2 . "</td></tr>";
$message .= "<tr><td><strong>Description:</strong> </td><td>" . strip_tags($desc) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";

    //$message = "Event Name: ".$eventName."\nDate: ".$newdate."\nTime: ".$startTime2." - ".$endTime2."\nDescription: ".$desc;

    mail($to,$subject,$message,$headers);

然而,电子邮件最终看起来像这样: enter image description here

html由于某种原因不起作用?

3 个答案:

答案 0 :(得分:5)

您需要对标头进行更改以指示其HTML电子邮件。试试这个:

$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: ".$from_address."\r\n";

更新:按照Matt Bryant的建议将\ n改为\ r \ n以符合rfc 2822规范。

答案 1 :(得分:2)

要发送HTML邮件,必须设置Content-type标头

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
// Additional headers
$headers .= "To: Mary <mary@example.com\r\n";
$headers .= "From: Birthday Reminder <birthday@example.com>\r\n";
$headers .= "Cc: birthdayarchive@example.com\r\n";
$headers .= "Bcc: birthdaycheck@example.com\r\n";

(php.net)

答案 2 :(得分:1)

添加正确的标题:

$headers = "From: ClubbedIn\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";