Built-in Mailform: Garbage in the Message Body

A place to report and discuss bugs - please mention CMSimple-version, server, platform and browser version
Post Reply
cmb
Posts: 13244
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Built-in Mailform: Garbage in the Message Body

Post by cmb » Thu Nov 01, 2012 10:38 pm

Hello Community,

I've received a report that a mail sent with CMSimple_XH's built-in mailform contained garbage in the message body. It seems to be related to the MTA, which either had needed a Content-Transfer-Encoding header or had problems with 8-bit characters. I've sent the following fix back to the user, and this worked fine. In cmsimple/mailform.php:

Code: Select all

function mail_utf8($to, $subject = '(No Subject)', $message = '', $header = '')
{
    $header = 'MIME-Version: 1.0' . "\r\n"
        . 'Content-Type: text/plain; charset=UTF-8' . "\r\n"
        . 'Content-Transfer-Encoding: base64' . "\r\n"
        . $header;
    $subject = '=?UTF-8?B?'
        . base64_encode(utf8_substr($subject, 0, 45)) . '?=';
        
    // word wrap the message giving preference to already existing line breaks
    $message = strtr(rtrim($message), array("\r\n" => "\n", "\r" => "\n"));
    $lines = explode("\n", $message);
    array_walk($lines,
               create_function('&$v, $i',
                               '$v = utf8_wordwrap($v, 72, "\n", true);'));
    $message = implode("\r\n", $lines);
    
    $message = chunk_split(base64_encode($message));
    
    return mail($to, $subject, $message, $header);
} 
This sets the Content-Transfer-Encoding header and encodes the message body with base64 according to RFC 2045. This way the mail will contain only 7-bit characters, so it should work everywhere.

The drawback: this base64 encoding has an overhead of about 35%. The alternative quoted-printable would have nearly no overhead for typical mails in English, estimated 10% for German, up to 500% for Russian and more than 1000% for several Asian languages. Additionally PHP offers a function to encode quoted-printable only since PHP 5.3.

I suggest that we include the bugfix in CMSimple_XH 1.5.6.

Christoph

PS: The amount of overhead given above is nonsense :oops: The overhead of base64 encoding is indeed about 35%. The overhead of quoted-printable is 200% in the worst case (texts that doesn't contain printable ASCII characters) , as each byte outside the ASCII range is encoded with 3 bytes.
Last edited by cmb on Mon Apr 08, 2013 4:22 pm, edited 1 time in total.
Reason: added PS
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 13244
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: Built-in Mailform: Garbage in the Message Body

Post by cmb » Tue Dec 03, 2013 1:45 pm

Christoph M. Becker – Plugins for CMSimple_XH

Post Reply