mail() 标头是否必须以换行符终止? (IncrediMail 问题)
我正在使用一个发送 UTF8 电子邮件的函数。 由于至少一个收件人对我的函数发送的电子邮件有问题(不是 UTF8,因此特殊字符被破坏;一些邮件标头出现在正文中),我想知道是否必须在标头字符串末尾再添加一个换行符:
function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "")
{
$header_ = 'MIME-Version: 1.0'."\r\n".'Content-type: text/plain; charset=UTF-8'."\r\n";
$header_ .= "From: [email protected]"; //Should be optional...
if (!empty($header)) $header_ .= "\r\n".$header;
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
return $out;
}
那么什么是正确的 - mail_utf8("[电子邮件受保护]"、""、"blah"、"")
或 mail_utf8("[电子邮件受保护]"、""、"blah"、"\r\n")
?
编辑:针对此类问题的明显资源 - www.php.net/mail - 仅在示例#4 中使用这样的换行符,与#2 不同。
Edit2:这是当前版本。请参阅评论以获取更多信息。
function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "")
{
$linebreak = PHP_EOL; //Seems to work everywhere, including IncrediMail
$linebreak = "\n"; //Debug
$header_ = 'MIME-Version: 1.0'.$linebreak.'Content-type: text/plain; charset=UTF-8'.$linebreak;
$header_ .= "From: [email protected]".$linebreak;
if (!empty($header)) $header_ .= $header.$linebreak; //Last line break !?
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
return $out;
}
I'm using a function for sending UTF8 emails.
Since at least one recipient has issues with emails sent by my function (not UTF8, so special chars broken; some mail headers appear in body), I'm wondering if I have to put another line break at the end of the header string:
function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "")
{
$header_ = 'MIME-Version: 1.0'."\r\n".'Content-type: text/plain; charset=UTF-8'."\r\n";
$header_ .= "From: [email protected]"; //Should be optional...
if (!empty($header)) $header_ .= "\r\n".$header;
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
return $out;
}
So what's correct - mail_utf8("[email protected]", "", "blah", "")
or mail_utf8("[email protected]", "", "blah", "\r\n")
?
Edit: The obvious resource for that kind of matter - www.php.net/mail - uses such a line break only in example #4, unlike in #2.
Edit2: So here's the current version. See comments for further info.
function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "")
{
$linebreak = PHP_EOL; //Seems to work everywhere, including IncrediMail
$linebreak = "\n"; //Debug
$header_ = 'MIME-Version: 1.0'.$linebreak.'Content-type: text/plain; charset=UTF-8'.$linebreak;
$header_ .= "From: [email protected]".$linebreak;
if (!empty($header)) $header_ .= $header.$linebreak; //Last line break !?
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
return $out;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试仅使用
\n
而不是\r\n
终止每个标题行。来源:PHP
mail()
函数Try terminating each header line with just
\n
, instead of\r\n
.Source: PHP
mail()
Function我认为你不能像在第 5 行那样在标题顶部传递换行符,并尝试避免使用大量引号,这看起来很混乱。
您的函数的重构示例:
I think that you can't pass a line break on top of header like you do on line 5, and try avoid alot of quotes, its look like confuse.
A re-factored exemple of your function: