简体   繁体   English

在php中发送html电子邮件时出现问题

[英]Problems sending html email in php

I'm trying to send an email to myself that has a layout and images. 我正在尝试向自己发送一封包含布局和图像的电子邮件。 What I'm I doing wrong? 我做错了什么?

<?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.";
}
?>

You should use phpmailer instead of PHP's mail()-Function. 您应该使用phpmailer而不是PHP的mail()-Function。 It allows you to easily send HTML-Mails. 它使您可以轻松发送HTML邮件。

Besides that you can try to validate your HTML-Code to be compatible for emailing. 除此之外,您可以尝试验证 HTML代码以兼容电子邮件。

Best wishes, Fabian 最好的祝福,费边

You have an error in your code: 您的代码中有错误:

WRONG 错误

$headers .= "Content-type: text/html\r\n";

RIGHT

$headers = "Content-type: text/html\r\n";

The .= throws a parse error in PHP unless you previously set $headers somewhere else. .=会在PHP中引发解析错误,除非您先前在其他地方设置了$headers

It also may depend on the email client you are testing with. 它还可能取决于您要测试的电子邮件客户端。 Be sure to check out http://www.email-standards.org/ to check what your email client supports. 请务必查看http://www.email-standards.org/来检查您的电子邮件客户端支持什么。

You may also want to look into Zend_Mail from Zend Framework: http://framework.zend.com/manual/en/zend.mail.html 您可能还想从Zend Framework中研究Zend_Mail: http : //framework.zend.com/manual/en/zend.mail.html

Would make dealing with headers, formats, MIME, etc. easier. 将使处理标头,格式,MIME等更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM