Jump to content

Problem with mail headers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
ReekenX

ReekenX

    Programmer

  • Members
  • PipPipPipPip
  • 134 posts
Hi everyone. I have my own PHP framework. 3 months of hard work. I have done 7 websites with framework, but two days ago, I have found strange bug.

I am using this function:

/**

 * Send mail

 *

 * @param  string $To          Send email to

 * @param  string $From        Send from

 * @param  string $Subject     Email subject

 * @param  string $Body        Email body

 * @param  string $Encoding    Encoding to use

 * @param  string $Type        Email type (html or plain)

 * @return void

 * @access public

 */

if (!function_exists("send_mail"))

{

    function send_mail($To, $From, $Subject, $Body, $Encoding = "iso-8859-1", $TYPE = "plain")

    {

        $Headers  = "From: " . $From . "\r\n";

        $Headers .= "Reply-To: " . $From . "\r\n";

        $Headers .= "Return-Path: " . $From . "\r\n";

        $Headers .= "Content-Type: text/" . $TYPE . "; charset=" . $Encoding . "\r\n";

        $Headers .= "X-mailer: ReekenX WForce Framework\r\n";

        mail($To, $Subject, $Body, $Headers);       

    }

}

The solution I have found is to replace \r\n with only \n. In 6 servers, my mail sending was correct, but in one, mail headers was seen in message body. Like:

From: sender@sender.com

Reply-to: sender@sender.com

(other headers seen here)


Message text


Can anybody explain why email sending with \r\n is bad, and with \n is right?

Sorry for my poor english :)
www.jarmalavicius.lt | www.github.com/reekenx | www.twitter.com/reekenx

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
\r is a carriage return (ASCII char 13), \n is a new line (ASCII char 10). Sometimes one or the other is required, sometimes both. It depends on the language used. I dunno why it isn't working ATM.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
According to the RFC2822, all mail headers should be terminated with a CRLF (carriage return line-feed), so it is not an issue with your code. I could be wrong, but I have read some linux servers replace a LF with a CRLF automatically, thus creating a double CR which results CRCRLF [\r\r\n] and thus is invalid.

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
PHP themselves recommend \r\n...however, they also state:

Quote

Note: 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


#5
ReekenX

ReekenX

    Programmer

  • Members
  • PipPipPipPip
  • 134 posts
Many thanks to everyone!
www.jarmalavicius.lt | www.github.com/reekenx | www.twitter.com/reekenx