1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

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
from django.core.mail.backends.base import BaseEmailBackend
from django.utils import six
class EmailBackend(BaseEmailBackend):
@@ -13,6 +14,14 @@ class EmailBackend(BaseEmailBackend):
self._lock = threading.RLock()
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):
"""Write all messages to the stream in a thread-safe way."""
if not email_messages:
@@ -22,9 +31,7 @@ class EmailBackend(BaseEmailBackend):
try:
stream_created = self.open()
for message in email_messages:
self.stream.write('%s\n' % message.message().as_string())
self.stream.write('-' * 79)
self.stream.write('\n')
self.write_message(message)
self.stream.flush() # flush after each message
msg_count += 1
if stream_created:

View File

@@ -38,6 +38,11 @@ class EmailBackend(ConsoleEmailBackend):
kwargs['stream'] = None
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):
"""Return a unique file name."""
if self._fname is None:
@@ -48,7 +53,7 @@ class EmailBackend(ConsoleEmailBackend):
def open(self):
if self.stream is None:
self.stream = open(self._get_filename(), 'a')
self.stream = open(self._get_filename(), 'ab')
return True
return False