Changed console and filebackend to use msg.as_bytes to output the data as it would get send via smtp.

This commit is contained in:
Florian Apolloner 2013-12-30 23:45:43 +01:00
parent 5dfd824d38
commit c988745cca
3 changed files with 33 additions and 24 deletions

View File

@ -5,6 +5,7 @@ import sys
import threading import threading
from django.core.mail.backends.base import BaseEmailBackend from django.core.mail.backends.base import BaseEmailBackend
from django.utils import six
class EmailBackend(BaseEmailBackend): class EmailBackend(BaseEmailBackend):
@ -13,6 +14,14 @@ class EmailBackend(BaseEmailBackend):
self._lock = threading.RLock() self._lock = threading.RLock()
super(EmailBackend, self).__init__(*args, **kwargs) super(EmailBackend, self).__init__(*args, **kwargs)
def write_message(self, message):
msg = message.message().as_bytes()
if six.PY3:
msg = msg.decode()
self.stream.write('%s\n' % msg)
self.stream.write('-' * 79)
self.stream.write('\n')
def send_messages(self, email_messages): def send_messages(self, email_messages):
"""Write all messages to the stream in a thread-safe way.""" """Write all messages to the stream in a thread-safe way."""
if not email_messages: if not email_messages:
@ -22,9 +31,7 @@ class EmailBackend(BaseEmailBackend):
try: try:
stream_created = self.open() stream_created = self.open()
for message in email_messages: for message in email_messages:
self.stream.write('%s\n' % message.message().as_string()) self.write_message(message)
self.stream.write('-' * 79)
self.stream.write('\n')
self.stream.flush() # flush after each message self.stream.flush() # flush after each message
msg_count += 1 msg_count += 1
if stream_created: if stream_created:

View File

@ -38,6 +38,11 @@ class EmailBackend(ConsoleEmailBackend):
kwargs['stream'] = None kwargs['stream'] = None
super(EmailBackend, self).__init__(*args, **kwargs) super(EmailBackend, self).__init__(*args, **kwargs)
def write_message(self, message):
self.stream.write(message.message().as_bytes() + b'\n')
self.stream.write(b'-' * 79)
self.stream.write(b'\n')
def _get_filename(self): def _get_filename(self):
"""Return a unique file name.""" """Return a unique file name."""
if self._fname is None: if self._fname is None:
@ -48,7 +53,7 @@ class EmailBackend(ConsoleEmailBackend):
def open(self): def open(self):
if self.stream is None: if self.stream is None:
self.stream = open(self._get_filename(), 'a') self.stream = open(self._get_filename(), 'ab')
return True return True
return False return False

View File

@ -2,7 +2,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import asyncore import asyncore
from email import message_from_file, message_from_string
from email.mime.text import MIMEText from email.mime.text import MIMEText
import os import os
import shutil import shutil
@ -20,16 +19,17 @@ from django.core.mail.backends import console, dummy, locmem, filebased, smtp
from django.core.mail.message import BadHeaderError from django.core.mail.message import BadHeaderError
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.test import override_settings from django.test import override_settings
from django.utils.encoding import force_str, force_text, force_bytes from django.utils.encoding import force_text, force_bytes
from django.utils.six import PY3, StringIO, string_types from django.utils.six import PY3, StringIO, binary_type
from django.utils.translation import ugettext_lazy from django.utils.translation import ugettext_lazy
if PY3: if PY3:
from email.utils import parseaddr from email.utils import parseaddr
from email import message_from_bytes from email import message_from_bytes, message_from_binary_file
else: else:
from email.Utils import parseaddr from email.Utils import parseaddr
message_from_bytes = message_from_string from email import (message_from_string as message_from_bytes,
message_from_file as message_from_binary_file)
class HeadersCheckMixin(object): class HeadersCheckMixin(object):
@ -42,13 +42,9 @@ class HeadersCheckMixin(object):
string with the contens of an email message. string with the contens of an email message.
:param headers: should be a set of (header-name, header-value) tuples. :param headers: should be a set of (header-name, header-value) tuples.
""" """
if isinstance(message, string_types): if isinstance(message, binary_type):
just_headers = message.split('\n\n', 1)[0] message = message_from_bytes(message)
hlist = just_headers.split('\n') msg_headers = set(message.items())
pairs = [hl.split(':', 1) for hl in hlist]
msg_headers = {(n, v.lstrip()) for (n, v) in pairs}
else:
msg_headers = set(message.items())
self.assertTrue(headers.issubset(msg_headers), msg='Message is missing ' self.assertTrue(headers.issubset(msg_headers), msg='Message is missing '
'the following headers: %s' % (headers - msg_headers),) 'the following headers: %s' % (headers - msg_headers),)
@ -694,9 +690,9 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
def get_mailbox_content(self): def get_mailbox_content(self):
messages = [] messages = []
for filename in os.listdir(self.tmp_dir): for filename in os.listdir(self.tmp_dir):
with open(os.path.join(self.tmp_dir, filename), 'r') as fp: with open(os.path.join(self.tmp_dir, filename), 'rb') as fp:
session = force_text(fp.read()).split('\n' + ('-' * 79) + '\n') session = fp.read().split(force_bytes('\n' + ('-' * 79) + '\n', encoding='ascii'))
messages.extend(message_from_string(force_str(m)) for m in session if m) messages.extend(message_from_bytes(m) for m in session if m)
return messages return messages
def test_file_sessions(self): def test_file_sessions(self):
@ -706,8 +702,8 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
connection.send_messages([msg]) connection.send_messages([msg])
self.assertEqual(len(os.listdir(self.tmp_dir)), 1) self.assertEqual(len(os.listdir(self.tmp_dir)), 1)
with open(os.path.join(self.tmp_dir, os.listdir(self.tmp_dir)[0])) as fp: with open(os.path.join(self.tmp_dir, os.listdir(self.tmp_dir)[0]), 'rb') as fp:
message = message_from_file(fp) message = message_from_binary_file(fp)
self.assertEqual(message.get_content_type(), 'text/plain') self.assertEqual(message.get_content_type(), 'text/plain')
self.assertEqual(message.get('subject'), 'Subject') self.assertEqual(message.get('subject'), 'Subject')
self.assertEqual(message.get('from'), 'from@example.com') self.assertEqual(message.get('from'), 'from@example.com')
@ -748,7 +744,7 @@ class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase):
self.stream = sys.stdout = StringIO() self.stream = sys.stdout = StringIO()
def get_mailbox_content(self): def get_mailbox_content(self):
messages = self.stream.getvalue().split(force_str('\n' + ('-' * 79) + '\n')) messages = self.stream.getvalue().split(str('\n' + ('-' * 79) + '\n'))
return [message_from_bytes(force_bytes(m)) for m in messages if m] return [message_from_bytes(force_bytes(m)) for m in messages if m]
def test_console_stream_kwarg(self): def test_console_stream_kwarg(self):
@ -758,14 +754,15 @@ class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase):
s = StringIO() s = StringIO()
connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s) connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s)
send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection) send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
self.assertMessageHasHeaders(s.getvalue(), { message = force_bytes(s.getvalue().split('\n' + ('-' * 79) + '\n')[0])
self.assertMessageHasHeaders(message, {
('MIME-Version', '1.0'), ('MIME-Version', '1.0'),
('Content-Type', 'text/plain; charset="utf-8"'), ('Content-Type', 'text/plain; charset="utf-8"'),
('Content-Transfer-Encoding', '7bit'), ('Content-Transfer-Encoding', '7bit'),
('Subject', 'Subject'), ('Subject', 'Subject'),
('From', 'from@example.com'), ('From', 'from@example.com'),
('To', 'to@example.com')}) ('To', 'to@example.com')})
self.assertIn('\nDate: ', s.getvalue()) self.assertIn(b'\nDate: ', message)
class FakeSMTPChannel(smtpd.SMTPChannel): class FakeSMTPChannel(smtpd.SMTPChannel):