diff options
| author | Claude Paroz <claude@2xlibre.net> | 2016-04-17 21:03:15 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2016-04-19 09:35:24 +0200 |
| commit | 836d475afefecd643d5e7f44027d7209df3ac690 (patch) | |
| tree | 868ff9939012e6d29dc911ab75fff7bed2ade24c /django | |
| parent | f43da05cc5c76102c04fd2e76b4bada880265dbe (diff) | |
Fixed #22561 -- Prevented too long lines in email messages
Thanks NotSqrt for the excellent report and Tim Graham for the review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/mail/message.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/django/core/mail/message.py b/django/core/mail/message.py index 546b895352..8dbc6b935c 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -25,11 +25,15 @@ from django.utils.encoding import force_text # some spam filters. utf8_charset = Charset.Charset('utf-8') utf8_charset.body_encoding = None # Python defaults to BASE64 +utf8_charset_qp = Charset.Charset('utf-8') +utf8_charset_qp.body_encoding = Charset.QP # Default MIME type to use on attachments (if it is not explicitly given # and cannot be guessed). DEFAULT_ATTACHMENT_MIME_TYPE = 'application/octet-stream' +RFC5322_EMAIL_LINE_LENGTH_LIMIT = 998 + class BadHeaderError(ValueError): pass @@ -169,7 +173,10 @@ class SafeMIMEText(MIMEMixin, MIMEText): # We do it manually and trigger re-encoding of the payload. MIMEText.__init__(self, _text, _subtype, None) del self['Content-Transfer-Encoding'] - self.set_payload(_text, utf8_charset) + has_long_lines = any(len(l) > RFC5322_EMAIL_LINE_LENGTH_LIMIT for l in _text.splitlines()) + # Quoted-Printable encoding has the side effect of shortening long + # lines, if any (#22561). + self.set_payload(_text, utf8_charset_qp if has_long_lines else utf8_charset) self.replace_header('Content-Type', 'text/%s; charset="%s"' % (_subtype, _charset)) elif _charset is None: # the default value of '_charset' is 'us-ascii' on Python 2 |
