我正在尝试使用PHP发送和HTML格式化的电子邮件。我一直很难拍,但这是我的测试代码:
$email ="
<html>
<body>
<p style=\"text-align:center;height:100px;background-color:#abc;border:1px solid #456;border-radius:3px;padding:10px;\">
<b>I am receiving HTML email</b>
<br/><br/><br/><a style=\"text-decoration:none;color:#246;\" href=\"www.example.com\">example</a>
</p>
<br/><br/>Now you Can send HTML Email
</body>
</html>";
$emailaddress = "personsemail@website.com";
$subject = "Test Email";
$headers = "From: noreply@server.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail($emailaddress, $subject, $email, $headers);
FROM标头有效,但电子邮件不会显示为HTML,而是显示如下所示的纯文本:
Content-type: text/html
Message-Id: <randomnumber@servername.hostingprovidor.com>
Date: Tue, 28 Apr 2015 12:45:48 -0500 (EST)
<html>
<body>
<p style="text-align:center;height:100px;background-color:#abc;border:1px solid #456;border-radius:3px;padding:10px;">
<b>I am receiving HTML email</b>
<br/><br/><br/><a style="text-decoration:none;color:#246;" href="www.example.com">example</a>
</p>
<br/><br/>Now you Can send HTML Email
</body>
</html>
这是服务器设置问题吗?谢谢你的帮助。
答案 0 :(得分:3)
您需要更多标题才能将电子邮件内容显示为html,请尝试以下方式:
$email = <<< LOL
<html>
<body>
<p style="text-align:center;height:100px;background-color:#abc;border:1px solid #456;border-radius:3px;padding:10px;">
<b>I am receiving HTML email</b>
<br/><br/><br/><a style="text-decoration:none;color:#246;" href="www.example.com">example</a>
</p>
<br/><br/>Now you Can send HTML Email
</body>
</html>
LOL;
$emailaddress = "personsemail@website.com";
$subject = "Test Email";
$headers = "From: noreply@server.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($emailaddress, $subject, $email, $headers);
答案 1 :(得分:1)
您应该在标头中设置MIME类型。试试这个:
$headers = 'From: noreply@server.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";