-
Notifications
You must be signed in to change notification settings - Fork 133
/
console.py
44 lines (36 loc) · 1.33 KB
/
console.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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