I'm using PHPMailer to send invoice emails. In some cases I get the undeliverable error of:
Your message contains invalid characters (bare line feed characters) which the email servers at something.com don't support.
There are other questions similar to this but none answer/solve my problem.
Like this: how to remove bare line feed characters using PHPMailer which does not have an answer.
After researching for few hour I came to conclusion that I have to replace LF ('\n') to CRLF ('\r\n').
I used the code below to replace any bare line feed characters to normalized line feeds in the email body and make it compliant to servers who does not accept bare line feeds. With the help of How to replace different newline styles in PHP the smartest way?
$body = preg_replace('~(*BSR_ANYCRLF)\R~', "\r\n", $body);
$body = preg_replace("#(?<!\r)\n#si", "\r\n", $body);
$body = preg_replace('~\R~u', "\r\n", $body);
Now:
after this change. Amount of undeliverable emails reduced, but the problem still exist. (getting undeliverable email couple times a week).
I thought this should work but I don't know how I'm still getting some undeliverable email errors when all the bare line feeds are replaced by the code above.
Note: Office 365 used to remove bare line feed characters before. But they changed it for security purposes.