Search code examples
phpemailsendmailpostfix-mtaline-endings

POSTFIX/sendmail suddenly adding extra line breaks


Over the weekend our MTA (POSTFIX) suddenly started prepending line breaks to the boundaries of our messages. We have several PHP templates that define multipart/alternative messages and define the headers. Here is the PHP mailer format, which was working friday, then just suddenly stopped on monday.

$headers = "From: name <our@example.com>\r\n" .
   "Reply-To: name <our@example.com>\r\n" .
   "MIME-Version: 1.0\r\n" .
   "Content-Type: multipart/alternative; boundary=\"09127kjhd821\"";

$txt = "\r\n\r\n--09127kjhd821\r\n" .
   "Content-Type: text/plain; charset=UTF-8\r\n" .
   "Content-Transfer-Encoding: quoted-printable\r\n\r\n" .
   "Text Message";

$html = "\r\n\r\n--09127kjhd821\r\n".
   "Content-Type: text/html; charset=UTF-8\r\n" .
   "Content-Transfer-Encoding: base64\r\n\r\n" .
   chunk_split( base64_encode( "HTML Message") );

$body = $txt . $html . "\r\n\r\n--09127kjhd821--";

mail(
   "someone@example.com",
   "=?UTF-8?B?" . base64_encode( "Subject" ) . "?=",
   $body,
   $headers
);

Comparing the original mails to the broken ones i see the following Broken:

Date: Fri,  3 Aug 2012 16:52:39 -0400 (EDT)





--09127kjhd821

Content-Type: text/plain; charset=UTF-8

Content-Transfer-Encoding: quoted-printable

Original (Working)

Date: Tue, 31 Jul 2012 12:36:45 -0400 (EDT)



--09127kjhd821
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

As you can see the line breaks are off pretty much doubling for each \r and \n, nothing was changed to my knowledge that would cause this.

Any suggestions or help is greatly appreciated.


Solution

  • The only thing I can come up with is the POSTFIX and PHP issues with converting LF to CRLF so when CRLF is defined in the message body for boundaries and headers it is being converted to CRCRLF. Saving the message source however only displays as CRLF in the hex editor though, which may be a conversion in the editor I use or email client download process.

    I still don't understand why it just suddenly changed as it was working fine previously. The only difference I can think of is maybe due to the scripts line-endings that may have changed from CRLF to LF causing a conversion to take place and thus breaking emails that contain CRLF in the body.

    I ultimately fixed the issue by changing the sendmail_path in php.ini to

    sendmail_path="/usr/bin/dos2unix|/usr/sbin/sendmail -t -i"