mail() 标头是否必须以换行符终止? (IncrediMail 问题)

发布于 2024-11-19 18:24:02 字数 1720 浏览 5 评论 0原文

我正在使用一个发送 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2024-11-26 18:24:02

尝试仅使用 \n 而不是 \r\n 终止每个标题行。

如果未收到消息,请尝试仅使用 LF (\n)。有些穷
优质 Unix 邮件传输代理自动将 LF 替换为 CRLF
(如果使用 CRLF,则会导致 CR 加倍)。这应该是最后一次
度假村,因为它不符合 » RFC 2822

来源:PHP mail() 函数

Try terminating each header line with just \n, instead of \r\n.

If messages are not received, try using a LF (\n) only. Some poor
quality Unix mail transfer agents replace LF by CRLF automatically
(which leads to doubling CR if CRLF is used). This should be a last
resort, as it does not comply with » RFC 2822.

Source: PHP mail() Function

天赋异禀 2024-11-26 18:24:02

我认为你不能像在第 5 行那样在标题顶部传递换行符,并尝试避免使用大量引号,这看起来很混乱。

您的函数的重构示例:

  function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "") {
    $header_  = "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n";
    $header_ .= "From: [email protected]\r\n";
    if(!empty($header)) $header_ .= $header;
    $out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
    return $out;
  }

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:

  function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "") {
    $header_  = "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n";
    $header_ .= "From: [email protected]\r\n";
    if(!empty($header)) $header_ .= $header;
    $out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
    return $out;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文