From 64c049b2711a66e12df114e06d2946949133e41e Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Thu, 14 Nov 2024 14:06:47 +0530 Subject: [PATCH 1/2] refactor: pass message as file to outbound API --- mail_client/mail_server.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mail_client/mail_server.py b/mail_client/mail_server.py index fe14fcdd..889addde 100644 --- a/mail_client/mail_server.py +++ b/mail_client/mail_server.py @@ -50,6 +50,7 @@ def request( params: dict | None = None, data: dict | None = None, json: dict | None = None, + files: dict | None = None, headers: dict[str, str] | None = None, timeout: int | tuple[int, int] = (60, 120), ) -> Any | None: @@ -60,12 +61,16 @@ def request( headers = headers or {} headers.update(self.client.headers) + if files: + headers.pop("content-type", None) + response = self.client.session.request( method=method, url=url, params=params, data=data, json=json, + files=files, headers=headers, timeout=timeout, ) @@ -114,13 +119,12 @@ class MailServerOutboundAPI(MailServerAPI): def send(self, outgoing_mail: str, recipients: str | list[str], message: str) -> str: """Sends an email message to the recipients using the Frappe Mail Server.""" + if isinstance(recipients, list): + recipients = ",".join(recipients) + endpoint = "/api/method/mail_server.api.outbound.send" - data = { - "outgoing_mail": outgoing_mail, - "recipients": recipients, - "message": message, - } - return self.request("POST", endpoint=endpoint, json=data) + data = {"outgoing_mail": outgoing_mail, "recipients": recipients} + return self.request("POST", endpoint=endpoint, data=data, files={"message": message}) def fetch_delivery_status(self, outgoing_mail: str, token: str) -> dict: """Fetches the delivery status of an email from the Frappe Mail Server.""" @@ -133,7 +137,7 @@ def fetch_delivery_statuses(self, data: list[dict]) -> list[dict]: """Fetches the delivery statuses of emails from the Frappe Mail Server.""" endpoint = "/api/method/mail_server.api.outbound.fetch_delivery_statuses" - return self.request("GET", endpoint=endpoint, json={"data": data}) + return self.request("POST", endpoint=endpoint, json=data) class MailServerInboundAPI(MailServerAPI): From 6011ebccb40820ced838cd134bb532a34a6c88f7 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Thu, 14 Nov 2024 14:59:58 +0530 Subject: [PATCH 2/2] chore: preserve recipients order --- .../mail_client/doctype/outgoing_mail/outgoing_mail.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.py b/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.py index 2d67e0d8..dc4629a2 100644 --- a/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.py +++ b/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.py @@ -666,7 +666,10 @@ def transfer_to_mail_server(self) -> None: message["X-Priority"] = "3" message = message.as_string() - recipients = list(set([rcpt.email for rcpt in self.recipients])) + # Remove duplicate recipients while preserving the order by using `dict.fromkeys()`. + # This avoids using a set, which could change the order of recipients. + recipients = list(dict.fromkeys([rcpt.email for rcpt in self.recipients])) + outbound_api = get_mail_server_outbound_api() token = outbound_api.send(self.name, recipients, message)