mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
* Un-hardcode status message_id in test backend For the test EmailBackend, get message ID's based on array position in `mail.outbox`, so that tests can predict the message ID. * Add a console backend for use in development Adds an EmailBackend derived from both Anymail's test backend and Django's console backend, to provide anymail statuses and signal handling while printing messages to the console. For use during development on localhost. Closes #87
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
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
|