| 1 |
""" |
| 2 |
Backend for test environment. |
| 3 |
""" |
| 4 |
|
| 5 |
import copy |
| 6 |
|
| 7 |
from django.core import mail |
| 8 |
from django.core.mail.backends.base import BaseEmailBackend |
| 9 |
|
| 10 |
|
| 11 |
class EmailBackend(BaseEmailBackend): |
| 12 |
""" |
| 13 |
An email backend for use during test sessions. |
| 14 |
|
| 15 |
The test connection stores email messages in a dummy outbox, |
| 16 |
rather than sending them out on the wire. |
| 17 |
|
| 18 |
The dummy outbox is accessible through the outbox instance attribute. |
| 19 |
""" |
| 20 |
|
| 21 |
def __init__(self, *args, **kwargs): |
| 22 |
super().__init__(*args, **kwargs) |
| 23 |
if not hasattr(mail, "outbox"): |
| 24 |
mail.outbox = [] |
| 25 |
|
| 26 |
def send_messages(self, messages): |
| 27 |
"""Redirect messages to the dummy outbox""" |
| 28 |
msg_count = 0 |
| 29 |
for message in messages: # .message() triggers header validation |
| 30 |
message.message() |
| 31 |
mail.outbox.append(copy.deepcopy(message)) |
| 32 |
msg_count += 1 |
| 33 |
return msg_count |