如何解决如何在php的电子邮件正文中发送html表?
| 我在电子邮件正文中发送动态创建的html表,但是在电子邮件中,我收到的是html代码而不是显示表格。请帮我。 这是我的代码。<?php
$output = \'<html><body><form>\';
$output .= \'<table border=\"1\"><tr><th>Author</th><th>Node Title</th><th>Node Summary</th><th>Node Body</th><th>Edit this node</th><th>Report Abuse</th><th>Group</th></tr>\';
while($row = mysql_fetch_array($sql)) {
$data = explode(\'\"\',$query[\'data\']);
$output .= \'<tr><td>\';
if($row[\'created\'] >= $strdate && $row[\'created\'] < $enddate) {
$output .= \'<a href=\"http://localhost/localstage/user/\' . $row[\'uid\'] . \'\">\' . $query[\'name\'] . \'</a></td><td>\';
$output .= \'<a href=\"http://localhost/localstage/node/\' . $row[\'nid\'] . \'\">\' . $row[\'title\'] . \'</a> (New)</td><td>\';
}
else {
$output .= \'<a href=\"http://localhost/localstage/user/\' . $sql_query[\'uid\'] . \'\">\' . $query[\'name\'] . \'</a></td><td>\';
$output .= \'<a href=\"http://localhost/localstage/node/\' . $row[\'nid\'] . \'\">\' . $row[\'title\'] . \'</a> (Updated)</td><td>\';
}
//$output .= \'</td><td>\';
$output .= $row[\'teaser\'] . \'</td><td>\';
if($row[\'field_provcomp_level_value\'] == 0 || $row[\'field_provcomp_level_value\'] == 1)<br /> {
$output .= $row[\'body\'] . \'</td><td>\';
}
else {
$output .= \'</td><td>\';
}
$output .= \'<a href=\"http://localhost/localstage/abuse/report/node/\' . $row[\'nid\'] . \'\">Abuse</a></td><td>\';
$output .= \'<a href=\"http://localhost/localstage/node/\' . $row[\'nid\'] . \'/edit\">Edit</a></td><td>\';
$query = mysql_fetch_array(mysql_query(\"SELECT title from node Where type = \'groupnode\' AND nid = \'\" . $row[\'nid\'] . \"\'\"));
$output .= $query[\'title\'] . \'</td></tr>\';
}
$output .= \'</table></form></body></html>\';
print $output;
$to = \'hmdnawaz@gmail.com\';
$headers = \"From Ahmad \\r\\n\";
//$headers .= \"Reply-To: \". strip_tags($_POST[\'req-email\']) . \"\\r\\n\";
//$headers .= \"CC: susan@example.com\\r\\n\";
$headers .= \'MIME-Version: 1.0\' . \"\\r\\n\";
$headers .= \'Content-Type: text/html; charset=ISO-8859-1\' . \"\\r\\n\";
$subject = \'Email report\';
mail($to,$subject,$output,$headers);
if(mail) {
echo \'Email sent\';
}
else {
echo \'Error sending email\';
}
?>
解决方法
首先,您在HTML代码中缺少标记,但这应该不是问题。
其次,您的标题不正确,您有:
$headers = \"From Ahmad \\r\\n\";
//$headers .= \"Reply-To: \". strip_tags($_POST[\'req-email\']) . \"\\r\\n\";
//$headers .= \"CC: susan@example.com\\r\\n\";
$headers .= \'MIME-Version: 1.0\' . \"\\r\\n\";
$headers .= \'Content-Type: text/html; charset=ISO-8859-1\' . \"\\r\\n\";
$subject = \'Email report\';
代替:
//$headers .= \"Reply-To: \". strip_tags($_POST[\'req-email\']) . \"\\r\\n\";
//$headers .= \"CC: susan@example.com\\r\\n\";
$headers = \'MIME-Version: 1.0\' . \"\\r\\n\";
$headers .= \'Content-Type: text/html; charset=ISO-8859-1\' . \"\\r\\n\";
$headers .= \"From: Ahmad <email@email.com>\\r\\n\";
$subject = \'Email report\';
基本上,我认为标题应该以\“ Mime-Version \”开头。
检查此代码:http://us.php.net/manual/en/function.mail.php#example-2877
, http://php.net/manual/zh/function.mail.php
发送HTML电子邮件的官方示例:
<?php
// multiple recipients
$to = \'aidan@example.com\' . \',\'; // note the comma
$to .= \'wez@example.com\';
// subject
$subject = \'Birthday Reminders for August\';
// message
$message = \'
<html>
<head>
<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>
\';
// To send HTML mail,the Content-type header must be set
$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>,Kelly <kelly@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\";
// Mail it
mail($to,$subject,$message,$headers);
?>
最后,一个问题可能是您创建的表树在特定的电子邮件客户端中存在一些渲染问题。你可以测试上面的代码吗?
, 首先,我建议使用Pear Mail扩展名(它随php的基本版本提供),因为使用mail()函数imo效果不佳,使用pear mail类,还可以设置哑剧类型容易。
Mail_mime类
请记住,许多邮件服务对HTML消息的支持有限(如果有),但是我不知道表的支持级别。
, 我建议Swiftmailer
http://swiftmailer.org/
它由Symfony项目使用,并具有出色的文档和支持。
不建议自己编写电子邮件功能,因为有很多怪癖和安全考虑。
这是设置html正文内容的文档:
http://swiftmailer.org/docs/message-body
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。