v0.145.2
 1"""Email backend that captures messages in memory for testing.
 2
 3Messages handed to ``send_messages`` are appended to the module-level
 4``outbox`` list instead of being delivered. Pair it with the ``mailoutbox``
 5pytest fixture shipped by this package, which clears ``outbox`` around each
 6test.
 7"""
 8
 9from __future__ import annotations
10
11from typing import TYPE_CHECKING
12
13from .base import BaseEmailBackend
14
15if TYPE_CHECKING:
16    from ..message import EmailMessage
17
18__all__ = ["EmailBackend", "outbox"]
19
20# Module-level, not per-instance: get_connection() builds a fresh backend for
21# every send, so captured mail has to accumulate somewhere shared.
22outbox: list[EmailMessage] = []
23
24
25class EmailBackend(BaseEmailBackend):
26    def send_messages(self, email_messages: list[EmailMessage]) -> int:
27        outbox.extend(email_messages)
28        return len(email_messages)