Skip to content

Commit

Permalink
fix: outgoing email account handlng (backport #24656) (#24657)
Browse files Browse the repository at this point in the history
* fix: Bail if no SMTP acccount can be found

Currently we give this weird error instead of just saying no outgoing email account is configured.

```
AttributeError: 'NoneType' object has no attribute 'get_smtp_server'
  File "frappe/email/queue.py", line 154, in flush
    email_queue.send()
  File "frappe/email/doctype/email_queue/email_queue.py", line 160, in send
    with SendMailContext(self, smtp_server_instance) as ctx:
  File "frappe/email/doctype/email_queue/email_queue.py", line 236, in __init__
    self.smtp_server: SMTPServer = smtp_server_instance or self.email_account_doc.get_smtp_server()
```

(cherry picked from commit 78665d9)

* fix: Fetch SMTP server inside context

Currently if smtp server creation fails email queue is endlessly
retried. It should just fail if there's no outgoing account.

(cherry picked from commit dae99eb)

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Feb 1, 2024
1 parent 22aa5d3 commit 4a59a01
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions frappe/email/doctype/email_queue/email_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ def to(self):
def attachments_list(self):
return json.loads(self.attachments) if self.attachments else []

def get_email_account(self):
def get_email_account(self, raise_error=False):
if self.email_account:
return frappe.get_cached_doc("Email Account", self.email_account)

return EmailAccount.find_outgoing(
match_by_email=self.sender, match_by_doctype=self.reference_doctype
match_by_email=self.sender, match_by_doctype=self.reference_doctype, _raise_error=raise_error
)

def is_to_be_sent(self):
Expand All @@ -158,6 +158,7 @@ def send(self, smtp_server_instance: SMTPServer = None):
return

with SendMailContext(self, smtp_server_instance) as ctx:
ctx.fetch_smtp_server()
message = None
for recipient in self.recipients:
if recipient.is_mail_sent():
Expand Down Expand Up @@ -231,14 +232,16 @@ def __init__(
smtp_server_instance: SMTPServer = None,
):
self.queue_doc: EmailQueue = queue_doc
self.email_account_doc = queue_doc.get_email_account()

self.smtp_server: SMTPServer = smtp_server_instance or self.email_account_doc.get_smtp_server()

self.smtp_server: SMTPServer = smtp_server_instance
self.sent_to_atleast_one_recipient = any(
rec.recipient for rec in self.queue_doc.recipients if rec.is_mail_sent()
)

def fetch_smtp_server(self):
self.email_account_doc = self.queue_doc.get_email_account(raise_error=True)
if not self.smtp_server:
self.smtp_server = self.email_account_doc.get_smtp_server()

def __enter__(self):
self.queue_doc.update_status(status="Sending", commit=True)
return self
Expand Down Expand Up @@ -730,7 +733,7 @@ def send_emails(self, queue_data, final_recipients):
recipients = list(set([r] + self.final_cc() + self.bcc))
q = EmailQueue.new({**queue_data, **{"recipients": recipients}}, ignore_permissions=True)
if not smtp_server_instance:
email_account = q.get_email_account()
email_account = q.get_email_account(raise_error=True)
smtp_server_instance = email_account.get_smtp_server()

with suppress(Exception):
Expand Down

0 comments on commit 4a59a01

Please sign in to comment.