rails/rails — pull request #16211 by sstephenson (2014-07-17 23:52:33+00:00)
Title: Default HTML email parts to Content-Transfer-Encoding: quoted-printable

**The problem**: If an email template generates a line longer than 998 characters, it is likely that the outgoing mail server will insert a line break after the 998th character.

In plain-text emails, this often results in a word split across two lines—ugly, but rarely a problem, since the convention is to hard-wrap at 72 columns.

But this behavior is particularly problematic in HTML emails, where e.g. rendering an autolinked paragraph can result in a long string of markup with the URL mangled by inadvertent whitespace.

**The explanation**: According to [RFC 2822](http://tools.ietf.org/html/rfc2822#section-2.1.1),

> Each line of characters [in a message body] MUST be no more than
> 998 characters, and SHOULD be no more than 78 characters, excluding
> the CRLF.

Many mail servers, notably Postfix, enforce this line limit by inserting a CRLF at position 999.

Action Mailer currently does not set a default Content-Transfer-Encoding for HTML email parts, and so the Mail library defaults to `7bit` or `8bit` accordingly. Message bodies in these encodings are raw and unmodified. That means if you generate a 1500-character line in your HTML template, that line goes through unmodified to the mail server.

Two alternative encodings solve this issue, each in a different way:

1. The `Base64` encoding is impervious to whitespace and can be wrapped at any column.
2. The `Quoted-Printable` encoding never generates lines longer than 76 characters.

**The solution**: Default HTML email parts to a Content-Transfer-Encoding of `Quoted-Printable`, as per [this post on the Postfix mailing list](https://groups.yahoo.com/neo/groups/postfix-users/conversations/topics/273296). Either encoding will work, but Quoted-Printable tends to be more space-efficient than Base64 for Latin-language HTML.