开发者

Do the mail() headers have to be terminated with a line break? (IncrediMail issue)

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: my@mail.com"; //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("x@y.z", "", "blah", "") or mail_utf8("x@y.z", "", "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: my@mail.com".$linebreak;
if (!empty($header)) $header_ .= $header.$linebreak; 开发者_如何学JAVA//Last line break !?
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);

return $out;
}


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


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: my@mail.com\r\n";
    if(!empty($header)) $header_ .= $header;
    $out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
    return $out;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜