Postfix 搞砸了电子邮件标题!
我已经在雪豹上设置了本地开发环境,并设置了 postfix 通过我的 isp 邮件服务器发送电子邮件。
经过多次挫折后,我最终让 postfix 开始工作,但现在当我的电子邮件发送时,标头信息被堵塞了!
我正在使用以下 php 代码:
$email = "me@mydomain"; $subject = "Email tester"; $body = "Simple test"; $header = "From: me@mydomain \r\n"; $header .= "MIME-VERSION: 1.0\r\n"; $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $phpversion = phpversion(); $header .= "X-Mailer: PHP v$phpversion\r\n"; mail($email,$subject,$body,$header); echo "message sent!";
“收件人:”和“主题:”标头按其应有的方式显示在标头中!,但其余部分显示在电子邮件正文中。这使得电子邮件看起来像是电子邮件客户端中的发件人字段为空。
我尝试过各种 php 脚本,有些非常简单,但都是一样的,标题总是显示在电子邮件正文中。
我认为这可能是后缀问题,但不确定,以前有人遇到过此类问题吗?
I've set up a local dev environment on snow leopard, and have set postfix up to send email via my isp mail server.
I eventually got postfix to work after much frustration, but now when my emails send the header information is bunged up!
I'm using the following php code:
$email = "me@mydomain"; $subject = "Email tester"; $body = "Simple test"; $header = "From: me@mydomain \r\n"; $header .= "MIME-VERSION: 1.0\r\n"; $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $phpversion = phpversion(); $header .= "X-Mailer: PHP v$phpversion\r\n"; mail($email,$subject,$body,$header); echo "message sent!";
The To: and Subject: headers display as they should, in the header!, but the rest display in the email body. This makes the email look like the from field in email client is empty.
I've tried a variety of php scripts, some very simple, but its the same thing, headers always displaying in the email body.
I'm thinking it could be a postfix problem, but not sure, anyone encountered this type of problem before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 *additional_headers* 中使用 PHP_EOL 而不是 \r\n,即示例中的 $header 。 PHP_EOL 将替换与您正在运行的操作系统相对应的换行符。
另外,消息应该只包含LN,即\n。这是根据 PHP 文档的。
每行应该用 LF (\n) 分隔。行不应超过 70 个字符。
确保您的脚本满足这两个条件 - 我已经尝试实现它,并最终让它在 Postfix 的默认配置下工作。
Use PHP_EOL instead of \r\n in *additional_headers*, i.e. $header in your example. PHP_EOL will substitute the newline correspondingly to the OS you are running on.
Also, message should contain LN only i.e. \n. This is accordingly to PHP documentation.
Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.
Make sure you meet both of criterias in your script - I've tried to achieve it and finally got it working with the default configuration of Postfix.
这几乎 100% 不是 Postfix 问题,而是由您的代码引起的。一旦标题后面出现空白 CRLF,正文就会开始。
您应该转储电子邮件正文,看看您是否无意中引入了额外的 CRLF。
This is almost 100% not a Postfix problem, but something caused by your code. The body starts once a blank CRLF is seen after the headers.
You should dump out your email body text and see if you're not accidentally introducing an extra CRLF.
进一步研究这个问题(基本上是因为我不想因此而改进大量脚本),我发现 PHP 和 Postfix 开发人员之间存在强烈的冲突,直到现在才得到解决。您可以在这里阅读广泛的信息:
http://www.mail- archive.com/[email protected]/msg03226.html
Postfix 预计在从以下地址接收邮件时 EOL 为 LF UNIX 上的 sendmail 和
发送时将其替换为 CRLF。 当它得到 CRLF 时,它仍然会替换
LF,我们得到 CRCRLF。
它解释了损坏的标头。为了解决这个问题,您必须确切地了解您的 Postfix 和 PHP/邮件系统是如何工作的。如果您遇到上述问题,只需使用“\n”而不是“\r\n”。如果你编写一个可以在 Windows/Unix 上运行的复杂系统,只需引入一个额外的参数,如 $eeol="\r\n";这将代替直接的“\r\n”标记,并且可以为任何系统轻松配置这种方式。
我想这就是 Postfix 作者推荐的方法:
如果 PHP 应用程序程序员以一致的方式格式化电子邮件消息,那就太好了。例如,他们可以使用包含 END-OF-LINE 终止符的变量,而不是到处硬编码 LF 或 CRLF 行终止符。
Investigating this problem further (basically because I didn't want to improve lots of scripts just because of that), I've come to the point that there is a strong conflict between PHP and Postfix developers, which is not fixed until now. You can read wide info here:
http://www.mail-archive.com/[email protected]/msg03226.html
Postfix expects EOL to be LF when picking up mail from sendmail on unix and
replaces that with CRLF when sending. When it gets CRLF it still replaces the
LF and we get CRCRLF.
It explains broken headers. To solve this, you must exactly know how your Postfix and PHP/mail system works. If you experience problems like described above, just use "\n" instead of "\r\n". If you program a complicated system which could run on both Windows/Unix, just introduce an extra param like $eeol="\r\n"; which will be put instead of direct "\r\n" tag and that way could be easily configured for any system.
I suppose that's the approach Postfix author recommends:
It would be really good if PHP application programmers formatted email messages in a consistent manner. For example, they could use a variable that contains the END-OF-LINE terminator, instead of hard-coding LF or CRLF line terminators all over the place.