我正在尝试向自己发送一封包含布局和图片的电子邮件。我做错了什么?
<?php
$message = $_POST['message'];
$emailsubject = 'site.com';
$webMaster = 'email@site.com';
$body = "
<html>
<body bgcolor=\"e7e7e7\">
<style type=\"text/css\">
#body {margin: auto;border: 0;padding: 0;font-family: Georgia, 'Times New Roman', Times, serif;font-size: 12px;}
#emailHeader {width: 500px;height: 131px;background: url(http://www.site.com/images/image.gif) no-repeat;}
#emailContent {width: 500px;background: url(http://www.site.com/images/image2.gif) repeat-y;text-align: left;padding: 0 33px 0 6px;}
#emailFooter {width: 500px;height: 34px;background: url(http://www.site.com/images/image3.gif) no-repeat;}
</style>
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td valign=\"top\" align=\"center\">
<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td id=\"emailHeader\"></td>
</tr>
<tr>
<td id=\"emailContent\">
content $message
</td>
</tr>
<tr>
<td id=\"emailFooter\"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>"
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailsubject, $body, $headers);
if ($success) {
echo "Your message was sent.";
}
else{
echo "There was a error.";
}
?>
答案 0 :(得分:3)
答案 1 :(得分:1)
您的代码中有错误:
<强> WRONG 强>
$headers .= "Content-type: text/html\r\n";
从右强>
$headers = "Content-type: text/html\r\n";
除非您之前在其他地方设置了.=
,否则$headers
会在PHP中抛出一个解析错误。
它还可能取决于您正在测试的电子邮件客户端。请务必查看http://www.email-standards.org/以查看您的电子邮件客户端支持的内容。
答案 2 :(得分:0)
您可能还想查看Zend Framework中的Zend_Mail: http://framework.zend.com/manual/en/zend.mail.html
可以更轻松地处理标题,格式,MIME等。