Plain is headed towards 1.0! Subscribe for development updates →

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