0

我正在尝试使用 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);

然而,电子邮件最终看起来像这样: 在此处输入图像描述

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

4

3 回答 3

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 规范。

于 2013-07-22T22:03:25.100 回答
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)

于 2013-07-22T22:08:45.813 回答
1

添加正确的标题:

$headers = "From: ClubbedIn\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
于 2013-07-22T22:04:11.363 回答