diff --git a/frappe/email/doctype/email_queue/email_queue.json b/frappe/email/doctype/email_queue/email_queue.json index ce8c3b5e63..b1df78c564 100644 --- a/frappe/email/doctype/email_queue/email_queue.json +++ b/frappe/email/doctype/email_queue/email_queue.json @@ -61,6 +61,7 @@ "options": "Not Sent\nSending\nSent\nPartially Sent\nError\nExpired" }, { + "depends_on": "eval:doc.error", "fieldname": "error", "fieldtype": "Code", "label": "Error" @@ -152,7 +153,7 @@ "idx": 1, "in_create": 1, "links": [], - "modified": "2023-06-05 12:15:17.850292", + "modified": "2023-06-07 02:21:28.769620", "modified_by": "Administrator", "module": "Email", "name": "Email Queue", diff --git a/frappe/email/doctype/email_queue/email_queue.py b/frappe/email/doctype/email_queue/email_queue.py index d977f2097c..b465c32eea 100644 --- a/frappe/email/doctype/email_queue/email_queue.py +++ b/frappe/email/doctype/email_queue/email_queue.py @@ -140,7 +140,9 @@ class EmailQueue(Document): method(self, self.sender, recipient.recipient, message) else: if not frappe.flags.in_test: - ctx.smtp_session.sendmail(from_addr=self.sender, to_addrs=recipient.recipient, msg=message) + ctx.smtp_server.session.sendmail( + from_addr=self.sender, to_addrs=recipient.recipient, msg=message + ) ctx.add_to_sent_list(recipient) if frappe.flags.in_test: @@ -217,19 +219,24 @@ class SendMailContext: smtplib.SMTPHeloError, JobTimeoutException, ] + trace = "".join(traceback.format_tb(exc_tb)) if exc_tb else None if not self.retain_smtp_session: self.smtp_server.quit() - self.log_exception(exc_type, exc_val, exc_tb) - if exc_type in exceptions: - update_fields = {"status": "Partially Sent" if self.sent_to else "Not Sent"} + update_fields = {"status": "Partially Sent" if self.sent_to else "Not Sent", "error": trace} elif exc_type: + update_fields = {"error": trace} if self.queue_doc.retry < get_email_retry_limit(): - update_fields = {"status": "Not Sent", "retry": self.queue_doc.retry + 1} + update_fields.update( + { + "status": "Partially Sent" if self.sent_to else "Not Sent", + "retry": self.queue_doc.retry + 1, + } + ) else: - update_fields = {"status": "Error"} + update_fields.update({"status": "Error"}) else: update_fields = { "status": "Sent", @@ -240,19 +247,6 @@ class SendMailContext: self.queue_doc.update_status(**update_fields, commit=True) - def log_exception(self, exc_type, exc_val, exc_tb): - if exc_type: - traceback_string = "".join(traceback.format_tb(exc_tb)) - traceback_string += f"\n Queue Name: {self.queue_doc.name}" - - self.queue_doc.log_error("Email sending failed", traceback_string) - - @property - def smtp_session(self): - if frappe.flags.in_test: - return - return self.smtp_server.session - def add_to_sent_list(self, recipient): # Update recipient status recipient.update_db(status="Sent", commit=True)