php PHP邮件html格式不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11994556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mail html format not working
提问by user1483799
Using php, and with this code I receive the email as a plain text, did I miss something? as I need to send formatted email which could contain links for example.
使用 php,并使用此代码以纯文本形式收到电子邮件,我错过了什么吗?因为我需要发送可能包含链接的格式化电子邮件。
$to = "[email protected]";
$subject = "Password Recovery";
$body = '
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: [email protected]\r\n"."X-Mailer: php";
if (mail($to, $subject, $body, $headers))
echo "Password recovery instructions been sent to your email<br>";
回答by andrewsi
You've re-set your headers:
您已重新设置标题:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: [email protected]\r\n"."X-Mailer: php";
You're missing a dot in that last line, which is over-writing the previous two:
您在最后一行中缺少一个点,它覆盖了前两行:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: [email protected]\r\n"."X-Mailer: php";
回答by GLES
Look at this example, this is sufficient to send mail in php:
看这个例子,这足以在php中发送邮件:
<?php
//change this to your email.
$to = "[email protected]";
$from = "[email protected]";
$subject = "Hello! This is HTML email";
//begin of HTML message
$message ="
<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>";
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
//options to send to cc+bcc
//$headers .= "Cc: [email][email protected][/email]";
//$headers .= "Bcc: [email][email protected][/email]";
// now lets send the email.
mail($to, $subject, $message, $headers);
echo "Message has been sent....!";
?>
回答by MazarD
I found the same problem with a specific mail server, in my case the solution was to set "\n" instead of "\r\n" as the end of line for the headers.
我在特定的邮件服务器上发现了同样的问题,在我的情况下,解决方案是将“\n”而不是“\r\n”设置为标题的行尾。
回答by Grassant
Even though your problem seems to be caused by re-assigning to $headers, I've ran into a similar problem and discovered the cause to be the double line-ending "/r/n". Some mail apps such as Bluemail, start the mail body (effectively ending the headers) after reading a double line ending.
尽管您的问题似乎是由重新分配给 $headers 引起的,但我遇到了类似的问题并发现原因是双行结尾“/r/n”。一些邮件应用程序(例如 Bluemail)在阅读双行结尾后开始邮件正文(有效地结束标题)。
In my case, it was solved by changing this:
在我的情况下,它是通过改变这个来解决的:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: [email protected]\r\n"."X-Mailer: php";
to this:
对此:
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers = "From: [email protected]\n"."X-Mailer: php";

