Skip to content

Commit

Permalink
feat: deliver email to mail client
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Nov 5, 2024
1 parent 108caee commit 60c486e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions mail_server/api/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def add_or_update_domain(domain_name: str, mail_client_host: str = None) -> dict
"domain_name": doc.domain_name,
"dns_records": doc.get_dns_records(),
"dkim_domain": get_root_domain_name(),
"inbound_token": doc.get_password("inbound_token"),
}
response["dkim_selector"], response["dkim_private_key"] = doc.get_dkim_selector_and_private_key()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

import frappe
import requests
from frappe import _
from frappe.model.document import Document
from frappe.utils import cint, now, time_diff_in_seconds, validate_email_address
Expand Down Expand Up @@ -95,6 +96,31 @@ def process_message(self) -> None:
self.processed_after = time_diff_in_seconds(self.processed_at, self.fetched_at)
self.db_update()

if self.status == "Accepted":
self.deliver_email_to_mail_client()

def deliver_email_to_mail_client(self):
"""Deliver email to mail client."""

domain_registry = frappe.get_cached_doc("Mail Domain Registry", self.domain_name)
if domain_registry.mail_client_host:
if not domain_registry.inbound_token:
return

host = domain_registry.mail_client_host
data = {
"oml": self.name,
"is_spam": self.is_spam,
"message": self.message,
"domain_name": self.domain_name,
"inbound_token": domain_registry.get_password("inbound_token"),
}

try:
requests.post(f"{host}/api/method/mail.api.webhook.receive_email", json=data)
except Exception:
frappe.log_error(title="Mail Client Email Delivery Failed", message=frappe.get_traceback())

def _db_set(
self,
update_modified: bool = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"is_subdomain",
"column_break_gjk4",
"last_verified_at",
"inbound_token",
"domain_owner",
"dkim_key_size",
"mail_client_host",
Expand Down Expand Up @@ -105,6 +106,13 @@
"fieldname": "mail_client_host",
"fieldtype": "Data",
"label": "Mail Client Host"
},
{
"fieldname": "inbound_token",
"fieldtype": "Password",
"label": "Inbound Token",
"no_copy": 1,
"read_only": 1
}
],
"index_web_pages_for_search": 1,
Expand All @@ -125,7 +133,7 @@
"link_fieldname": "domain_name"
}
],
"modified": "2024-10-30 18:19:40.233197",
"modified": "2024-11-05 12:23:43.351978",
"modified_by": "Administrator",
"module": "Mail Server",
"name": "Mail Domain Registry",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def validate(self) -> None:
self.validate_domain_owner()
self.validate_dkim_key_size()

if self.is_new():
self.inbound_token = frappe.generate_hash(length=32)

if self.is_new() or self.has_value_changed("dkim_key_size"):
self.last_verified_at = now()
validate_mail_server_settings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def update_delivery_status_in_mail_client(self) -> None:
if host := frappe.get_cached_value("Mail Domain Registry", self.domain_name, "mail_client_host"):
data = self.get_delivery_status()
try:
requests.post(f"{host}/api/method/mail.api.outbound.update_delivery_status", json=data)
requests.post(f"{host}/api/method/mail.api.webhook.update_delivery_status", json=data)
except Exception:
frappe.log_error(
title="Mail Client Delivery Status Update Failed", message=frappe.get_traceback()
Expand Down Expand Up @@ -256,7 +256,7 @@ def push_to_queue(self) -> None:
"""Pushes the email to the queue for sending."""

if not frappe.flags.force_push_to_queue:
self.load_from_db()
self.reload()

# Ensure the document is "Accepted"
if not (self.status == "Accepted" and self.failed_count < 3):
Expand Down

0 comments on commit 60c486e

Please sign in to comment.