Skip to content

Commit

Permalink
Merge pull request #30 from s-aga-r/refactor-apis
Browse files Browse the repository at this point in the history
refactor: pass message as file to outbound API
  • Loading branch information
s-aga-r authored Nov 14, 2024
2 parents ca072a2 + 6011ebc commit 4718743
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 11 additions & 7 deletions mail_client/mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
)
Expand Down Expand Up @@ -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."""
Expand All @@ -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):
Expand Down

0 comments on commit 4718743

Please sign in to comment.