iOS Mail app small mails encoded to 7bit ASCII/UTF-8 - Solve with Roundcube 0.9.5

Hi!

We have iPads that had difficulty viewing certain content from Roundcube [RC], and we've mended this problem with help from our RoundCube community. This issue is present with Roundcube 0.9.5 and is fixed in future releases (this post edited in 2019).

Lets frame our problem If we send emails from RC with specific default encoding then iPad's Mail programme displays this silly message,

    This message cannot be displayed because of the way it is formatted.
    Ask the sender to send it again using a different format or email program. multipart/alternative
			

How did this happen, and why? RC sends ASCII text with misleading information, because messages were sent with 7 bit ASCII chars within UTF-8 ranges.
If so and they have less than 1000 characters then they could be encoded to 7bit ASCII/UTF-8! Incorrectly RC specifies 8b it UTF-8 when Content-Transfer-Encoding is 7bit. So weird, so here it is,

    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain; charset=UTF-8

I ran several tests and found our iPad users can read emails if we replace

    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain; charset=UTF-8

with

    Content-Transfer-Encoding: quoteable-printable
    Content-Type: text/plain; charset=UTF-8
			

Hacks If I could not change how RC behaves then I should try replacing text with something like MIMEDfang because I did not think MTAs able or appropriate for this job. However sadly they would all be hacks and we should have a solution not ugly workarounds :(

Solutions I brought this to attention of the RC users mailing list, and asked if or how I should modify RC for this. RC encoding is performed by mime.php and this should be modified. After discussion on the RC mailing list Kaz Kylheku kindly wrote a patch for the mime.php and I'm so happy to report this worked! If you suffer from the same problems, then you'll find the patch beneath, and with instructions! Thank-you Kaz!

If you would like to send 7bit ASCII message then please don't specify 8bit character sets. I think Apple were morally right to treat such emails invalid, but I think feel they should have allowed this if they'd taken pragmatic approaches, because all other Email clients would have displayed them.

Patch for RoundCube I've tested his patch with 0.9.5, and likely this should run on all 0.9 releases, and if code in mime.php did not change lots then please try on earlier versions. If the text encoding 7bit, and the charset is not ASCII, then the transfer encoding becomes ""quoted-printable"" without trying to be clever and checking that the body is all in the 7 bit range. Clever checking should not be needed because RC should have limited the UTF-8 range initially for it to send in 7-bit ASCII. Please don't forget to back up your mime.php, and save this patch into a file somewhere. I put this into my roundcube installation directory, and named this mime.php.patch, and then I cd'ed into my roundcube installtion directory and ran the command,

    # patch -p1  < program/lib/Mail/mime.php.patch
    patching file program/lib/Mail/mime.php
    Index: roundcube/program/lib/Mail/mime.php
    ===================================================================
    --- roundcube.orig/program/lib/Mail/mime.php    2013-11-27 12:44:24.000000000 -0800
    +++ roundcube/program/lib/Mail/mime.php 2013-11-27 12:47:42.000000000 -0800
    @@ -1459,14 +1459,12 @@
            // text body
    if ($this->_build_params['text_encoding'] == '7bit'
            && !preg_match('/ascii/i', $this->_build_params['text_charset'])
    -            && preg_match('/[^\x00-\x7F]/', $this->_txtbody)
    ) {
      $this->_build_params['text_encoding'] = 'quoted-printable';
    // html body
    if ($this->_build_params['html_encoding'] == '7bit'
    && !preg_match('/ascii/i', $this->_build_params['html_charset'])
    -            && preg_match('/[^\x00-\x7F]/', $this->_htmlbody)
     ) {
         $this->_build_params['html_encoding'] = 'quoted-printable';
     }

Good luck and bye :)