2009-11-03 20:53:26 +08:00
|
|
|
"""
|
|
|
|
Backend for test environment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.core import mail
|
|
|
|
from django.core.mail.backends.base import BaseEmailBackend
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
class EmailBackend(BaseEmailBackend):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""
|
|
|
|
An email backend for use during test sessions.
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
The test connection stores email messages in a dummy outbox,
|
|
|
|
rather than sending them out on the wire.
|
|
|
|
|
|
|
|
The dummy outbox is accessible through the outbox instance attribute.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2009-11-03 20:53:26 +08:00
|
|
|
if not hasattr(mail, 'outbox'):
|
|
|
|
mail.outbox = []
|
|
|
|
|
|
|
|
def send_messages(self, messages):
|
|
|
|
"""Redirect messages to the dummy outbox"""
|
2013-04-04 10:42:31 +08:00
|
|
|
msg_count = 0
|
2012-09-22 21:17:13 +08:00
|
|
|
for message in messages: # .message() triggers header validation
|
|
|
|
message.message()
|
2016-08-09 06:29:55 +08:00
|
|
|
mail.outbox.append(message)
|
2013-04-04 10:42:31 +08:00
|
|
|
msg_count += 1
|
|
|
|
return msg_count
|