Wednesday, December 9, 2009

Bare LFs in SMTP, escaping newlines in mail

You might have received the page http://cr.yp.to/docs/smtplf.html when trying to send mail using the PHP mail function. The reason is that linebreaks must be escaped in emails.

Usually a linebreak is a \n (or LF, line feed), however when sending mails the standard says this LF must be escaped with a \r (or CR, carriage return). so \n shuold be \r\n

Escaping the email text like the following will do the trick:
$escapedString = str_replace("\r\r\n", "\r\n", str_replace("\n", "\r\n", $unescapedString));

No comments:

Post a Comment