Skip to content

Commit f0181c7

Browse files
committed
Mailtrap backend: update payload to keep track of recipient ordering
1 parent 16abb25 commit f0181c7

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

anymail/backends/mailtrap.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def __init__(
6262
# Yes, the parent sets this, but setting it here, too, gives type hints
6363
self.backend = backend
6464
self.metadata = None
65+
66+
# needed for backend.parse_recipient_status
67+
self.recipients_to: List[str] = []
68+
self.recipients_cc: List[str] = []
69+
self.recipients_bcc: List[str] = []
70+
6571
super().__init__(
6672
message, defaults, backend, *args, headers=http_headers, **kwargs
6773
)
@@ -99,11 +105,21 @@ def _mailtrap_email(email: EmailAddress) -> MailtrapAddress:
99105
def set_from_email(self, email: EmailAddress):
100106
self.data["from"] = self._mailtrap_email(email)
101107

102-
def add_recipient(
103-
self, recipient_type: Literal["to", "cc", "bcc"], email: EmailAddress
108+
def set_recipients(
109+
self, recipient_type: Literal["to", "cc", "bcc"], emails: List[EmailAddress]
104110
):
105111
assert recipient_type in ["to", "cc", "bcc"]
106-
self.data.setdefault(recipient_type, []).append(self._mailtrap_email(email))
112+
if emails:
113+
self.data[recipient_type] = [
114+
self._mailtrap_email(email) for email in emails
115+
]
116+
117+
if recipient_type == "to":
118+
self.recipients_to = [email.addr_spec for email in emails]
119+
elif recipient_type == "cc":
120+
self.recipients_cc = [email.addr_spec for email in emails]
121+
elif recipient_type == "bcc":
122+
self.recipients_bcc = [email.addr_spec for email in emails]
107123

108124
def set_subject(self, subject):
109125
self.data["subject"] = subject
@@ -228,9 +244,12 @@ def parse_recipient_status(
228244
email_message=message, payload=payload, response=response, backend=self
229245
)
230246

231-
# Not the best status reporting. Mailtrap only says that the order of
232-
# message-ids will be in this order (but JSON is unordered?)
233-
recipient_status_order = [*message.to, *message.cc, *message.bcc]
247+
# message-ids will be in this order
248+
recipient_status_order = [
249+
*payload.recipients_to,
250+
*payload.recipients_cc,
251+
*payload.recipients_bcc,
252+
]
234253
recipient_status = {
235254
email: AnymailRecipientStatus(
236255
message_id=message_id,

0 commit comments

Comments
 (0)