Unexpected Exclamation Marks: Php Mail Function

by Vincenzo Gambino 

I had recently been working with PHP’s mail function(), sending out automated HTML e-mails when a form was submitted.

In the past this has always been fine when sending plain text e-mails, and I don’t usually send PHP e-mails in HTML. But, there were problems on various computers in the office. Some mail clients rendered the entire e-mail correctly, whereas others would add mysterious exclamation marks “!” to random areas of the e-mail.

Obviously this had to be fixed, so I did some research to find out more about the problem.

3.5 Overall message syntax

A message consists of header fields, optionally followed by a message body. Lines in a message MUST be a maximum of 998 characters excluding the CRLF, but it is RECOMMENDED that lines be limited to 78 characters excluding the CRLF.

That excerpt comes from the RFC2882 – Internet Message Format Specification

Here is what you have to do to fix this error.

Add a "Content-Transfer-Encoding: base64" header
Encode the body content with base64...
$base64contents = rtrim(chunk_split(base64_encode($contents)));

The problem with the exclamation marks should now be resolved.