This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vog
Recipients barry, r.david.murray, vog
Date 2015-12-25.10:33:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1451039623.64.0.603876276349.issue25948@psf.upfronthosting.co.za>
In-reply-to
Content
The email.mime package creates invalid MIME encoding (line too long) on certain inputs. That is, by proper usage of the email.mime API it is possible to create invalid emails that will be rejected by most mail servers.


Steps to reproduce
==================

1. Create a string that has the following properties:

1.1. The string contains only US-ASCII characters
1.2. The string contains a line that is longer than ~1000 characters

(For example, this could be an english-language HTML document that contains embedded images as "data:" URIs.)

2. Create MIMEText instance with that text, don't specify a charset

(This will default to 'us-ascii'. Alternatively, specify the 'us-ascii' charset explicitly.)

3. Create Message out of that MIMEText instance

4. Serialize the message


Expected result
===============

A MIME encoded email message that conforms to RFC 5322, section 2.1.1.: "Each line of characters MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF."


Actual result
=============

A MIME encoded email message that contains a line with more than 998 characters.  Hence, the email is rightfully rejected by most mail servers.


Workaround
==========

Specify the UTF-8 charset explicitly.  This forces MIMEText to switch to Base64 encoding, which will generate lines no longer than 78 characters.

For example, replace the following constructions:

MIMEText('Text with very long line', 'plain')
MIMEText('HTML with very long line', 'html')

with:

MIMEText('Text with very long line', 'plain', 'utf-8')
MIMEText('HTML with very long line', 'html', 'utf-8')


References
==========

- RFC 5322, 2.1.1. Line Length Limits
  https://tools.ietf.org/html/rfc5322#section-2.1.1

- RFC 2822, 2.1.1. Line Length Limits
  https://tools.ietf.org/html/rfc2822#section-2.1.1


Related issues
==============

- Django #22561: EmailMessage should respect RFC2822 on max line length
  https://code.djangoproject.com/ticket/22561

- Python #9298: binary email attachment issue with base64 encoding
  https://bugs.python.org/issue9298
History
Date User Action Args
2015-12-25 10:33:43vogsetrecipients: + vog, barry, r.david.murray
2015-12-25 10:33:43vogsetmessageid: <1451039623.64.0.603876276349.issue25948@psf.upfronthosting.co.za>
2015-12-25 10:33:43voglinkissue25948 messages
2015-12-25 10:33:42vogcreate