Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions anymail/backends/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import uuid
from django.core.mail.backends.console import EmailBackend as DjangoConsoleBackend

from ..exceptions import AnymailError
from .test import EmailBackend as AnymailTestBackend


class EmailBackend(AnymailTestBackend, DjangoConsoleBackend):
"""
Anymail backend that prints messages to the console, while retaining
anymail statuses and signals.
"""

esp_name = "Console"

def get_esp_message_id(self, message):
# Generate a guaranteed-unique ID for the message
return str(uuid.uuid4())

def send_messages(self, email_messages):
if not email_messages:
return
msg_count = 0
with self._lock:
try:
stream_created = self.open()
for message in email_messages:
try:
sent = self._send(message)
except AnymailError:
if self.fail_silently:
sent = False
else:
raise
if sent:
self.write_message(message)
self.stream.flush() # flush after each message
msg_count += 1
finally:
if stream_created:
self.close()

return msg_count
10 changes: 9 additions & 1 deletion anymail/backends/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def __init__(self, *args, **kwargs):
if not hasattr(mail, 'outbox'):
mail.outbox = [] # see django.core.mail.backends.locmem

def get_esp_message_id(self, message):
# Get a unique ID for the message. The message must have been added to
# the outbox first.
return mail.outbox.index(message)

def build_message_payload(self, message, defaults):
return TestPayload(backend=self, message=message, defaults=defaults)

Expand All @@ -41,7 +46,10 @@ def post_to_esp(self, payload, message):
raise response
except AttributeError:
# Default is to return 'sent' for each recipient
status = AnymailRecipientStatus(message_id=1, status='sent')
status = AnymailRecipientStatus(
message_id=self.get_esp_message_id(message),
status='sent'
)
response = {
'recipient_status': {email: status for email in payload.recipient_emails}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_send_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def handle_post_send(sender, message, status, esp_name, **kwargs):
self.assertEqual(sender, TestEmailBackend)
self.assertEqual(message, self.message)
self.assertEqual(status.status, {'sent'})
self.assertEqual(status.message_id, 1) # TestEmailBackend default message_id
self.assertEqual(status.message_id, 0)
self.assertEqual(status.recipients['to@example.com'].status, 'sent')
self.assertEqual(status.recipients['to@example.com'].message_id, 1)
self.assertEqual(status.recipients['to@example.com'].message_id, 0)
self.assertEqual(esp_name, "Test") # the TestEmailBackend's ESP is named "Test"
self.receiver_called = True
self.addCleanup(post_send.disconnect, receiver=handle_post_send)
Expand Down