mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
[py3] Fixed mail tests with Python 3
This commit is contained in:
@@ -11,11 +11,10 @@ from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.base import MIMEBase
|
||||
from email.header import Header
|
||||
from email.utils import formatdate, getaddresses, formataddr, parseaddr
|
||||
from io import BytesIO
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail.utils import DNS_NAME
|
||||
from django.utils.encoding import smart_bytes, force_text
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils import six
|
||||
|
||||
|
||||
@@ -83,34 +82,34 @@ def forbid_multi_line_headers(name, val, encoding):
|
||||
if '\n' in val or '\r' in val:
|
||||
raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))
|
||||
try:
|
||||
val = val.encode('ascii')
|
||||
val.encode('ascii')
|
||||
except UnicodeEncodeError:
|
||||
if name.lower() in ADDRESS_HEADERS:
|
||||
val = ', '.join(sanitize_address(addr, encoding)
|
||||
for addr in getaddresses((val,)))
|
||||
else:
|
||||
val = str(Header(val, encoding))
|
||||
val = Header(val, encoding).encode()
|
||||
else:
|
||||
if name.lower() == 'subject':
|
||||
val = Header(val)
|
||||
return smart_bytes(name), val
|
||||
val = Header(val).encode()
|
||||
return str(name), val
|
||||
|
||||
|
||||
def sanitize_address(addr, encoding):
|
||||
if isinstance(addr, six.string_types):
|
||||
addr = parseaddr(force_text(addr))
|
||||
nm, addr = addr
|
||||
nm = str(Header(nm, encoding))
|
||||
nm = Header(nm, encoding).encode()
|
||||
try:
|
||||
addr = addr.encode('ascii')
|
||||
addr.encode('ascii')
|
||||
except UnicodeEncodeError: # IDN
|
||||
if '@' in addr:
|
||||
localpart, domain = addr.split('@', 1)
|
||||
localpart = str(Header(localpart, encoding))
|
||||
domain = domain.encode('idna')
|
||||
domain = domain.encode('idna').decode('ascii')
|
||||
addr = '@'.join([localpart, domain])
|
||||
else:
|
||||
addr = str(Header(addr, encoding))
|
||||
addr = Header(addr, encoding).encode()
|
||||
return formataddr((nm, addr))
|
||||
|
||||
|
||||
@@ -132,7 +131,7 @@ class SafeMIMEText(MIMEText):
|
||||
This overrides the default as_string() implementation to not mangle
|
||||
lines that begin with 'From '. See bug #13433 for details.
|
||||
"""
|
||||
fp = BytesIO()
|
||||
fp = six.StringIO()
|
||||
g = Generator(fp, mangle_from_ = False)
|
||||
g.flatten(self, unixfrom=unixfrom)
|
||||
return fp.getvalue()
|
||||
@@ -156,7 +155,7 @@ class SafeMIMEMultipart(MIMEMultipart):
|
||||
This overrides the default as_string() implementation to not mangle
|
||||
lines that begin with 'From '. See bug #13433 for details.
|
||||
"""
|
||||
fp = BytesIO()
|
||||
fp = six.StringIO()
|
||||
g = Generator(fp, mangle_from_ = False)
|
||||
g.flatten(self, unixfrom=unixfrom)
|
||||
return fp.getvalue()
|
||||
@@ -210,8 +209,7 @@ class EmailMessage(object):
|
||||
|
||||
def message(self):
|
||||
encoding = self.encoding or settings.DEFAULT_CHARSET
|
||||
msg = SafeMIMEText(smart_bytes(self.body, encoding),
|
||||
self.content_subtype, encoding)
|
||||
msg = SafeMIMEText(self.body, self.content_subtype, encoding)
|
||||
msg = self._create_message(msg)
|
||||
msg['Subject'] = self.subject
|
||||
msg['From'] = self.extra_headers.get('From', self.from_email)
|
||||
@@ -293,7 +291,7 @@ class EmailMessage(object):
|
||||
basetype, subtype = mimetype.split('/', 1)
|
||||
if basetype == 'text':
|
||||
encoding = self.encoding or settings.DEFAULT_CHARSET
|
||||
attachment = SafeMIMEText(smart_bytes(content, encoding), subtype, encoding)
|
||||
attachment = SafeMIMEText(content, subtype, encoding)
|
||||
else:
|
||||
# Encode non-text attachments with base64.
|
||||
attachment = MIMEBase(basetype, subtype)
|
||||
@@ -313,9 +311,11 @@ class EmailMessage(object):
|
||||
attachment = self._create_mime_attachment(content, mimetype)
|
||||
if filename:
|
||||
try:
|
||||
filename = filename.encode('ascii')
|
||||
filename.encode('ascii')
|
||||
except UnicodeEncodeError:
|
||||
filename = ('utf-8', '', filename.encode('utf-8'))
|
||||
if not six.PY3:
|
||||
filename = filename.encode('utf-8')
|
||||
filename = ('utf-8', '', filename)
|
||||
attachment.add_header('Content-Disposition', 'attachment',
|
||||
filename=filename)
|
||||
return attachment
|
||||
|
||||
Reference in New Issue
Block a user