Opened 8 years ago

Closed 8 years ago

#27333 closed Bug (fixed)

EmailMessage.message().as_string() incorrectly base64 encoded on Python 3

Reported by: aRkadeFR Owned by: Claude Paroz
Component: Core (Mail) Version: dev
Severity: Normal Keywords: email, base64
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a different behavior on body encoding on python 2.7.12 and python 3.5.2 with Django 1.8.15:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

from django.conf import settings
settings.configure(DATABASES={}, SECRET_KEY='not empty')

from django.core.mail import EmailMessage


email = EmailMessage('Chère chère', 'Super \' fort très fort.',
                     'from@example.com', ['to@example.com'])
print(email.message().as_string())

On python 3, the body will be encoded as base64, but not on python 2.

I've found couple of tickets relating to the same subject (#3472, and #11212 among others), and I've found in the Django code 1.8.15 in the file django/core/mail/message.py:

# Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
# some spam filters.
utf8_charset = Charset.Charset('utf-8')
utf8_charset.body_encoding = None  # Python defaults to BASE64

I would think from the code and the tickets that Django tries to never base64 the body of EmailMessage.

Is it the desired behavior?

Change History (4)

comment:1 by Claude Paroz, 8 years ago

Owner: changed from nobody to Claude Paroz
Status: newassigned
Triage Stage: UnreviewedAccepted
Version: 1.8master

Looks like a legitimate bug (I reproduced on master). The charset dance we're doing in SafeMIMEText.__init__ isn't sufficient for all cases, apparently.

comment:2 by Claude Paroz, 8 years ago

Has patch: set

comment:3 by Tim Graham, 8 years ago

Summary: Different behavior py2 / py3 with Django on base64 body_encoding EmailMessageEmailMessage.message().as_string() incorrectly base64 encoded on Python 3
Triage Stage: AcceptedReady for checkin

comment:4 by Claude Paroz <claude@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 458e2fbf:

Fixed #27333 -- Prevented BASE64 encoding in message.as_string() on Python 3

Thanks Tim Graham for the review.

Note: See TracTickets for help on using tickets.
Back to Top