From 1b1479ef378f40f2b95e380d1f0b5d648061bd54 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 7 Jun 2022 13:03:19 +0530 Subject: [PATCH] fix(email): Maintain multi-tenancy translation sanity in error messages Defining translated strings as globals means the strings are evaluated at import time. So the value of these variables is set based on which site or user initializes the module. This makes it possible for an english user to get Russian errors. --- Other changes: Remove unreachable code, avoid importing _socket.error alias to OSError --- .../assignment_rule/assignment_rule.py | 2 - frappe/core/doctype/communication/email.py | 15 +++---- .../doctype/email_account/email_account.py | 6 +-- frappe/email/smtp.py | 41 ++++++++----------- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/frappe/automation/doctype/assignment_rule/assignment_rule.py b/frappe/automation/doctype/assignment_rule/assignment_rule.py index f3dfa4cf0a..0ca64e54c2 100644 --- a/frappe/automation/doctype/assignment_rule/assignment_rule.py +++ b/frappe/automation/doctype/assignment_rule/assignment_rule.py @@ -298,8 +298,6 @@ def apply(doc=None, method=None, doctype=None, name=None): if reopened: break - # print(f"Rule:{assignment_rule}\nDoc: {doc}\nReOpened: {reopened}") - assignment_rule.close_assignments(doc) diff --git a/frappe/core/doctype/communication/email.py b/frappe/core/doctype/communication/email.py index 887037dca1..2c8a65fafe 100755 --- a/frappe/core/doctype/communication/email.py +++ b/frappe/core/doctype/communication/email.py @@ -22,14 +22,6 @@ if TYPE_CHECKING: from frappe.core.doctype.communication.communication import Communication -OUTGOING_EMAIL_ACCOUNT_MISSING = _( - """ - Unable to send mail because of a missing email account. - Please setup default Email Account from Setup > Email > Email Account -""" -) - - @frappe.whitelist() def make( doctype=None, @@ -170,7 +162,12 @@ def _make( if cint(send_email): if not comm.get_outgoing_email_account(): - frappe.throw(msg=OUTGOING_EMAIL_ACCOUNT_MISSING, exc=frappe.OutgoingEmailError) + frappe.throw( + _( + "Unable to send mail because of a missing email account. Please setup default Email Account from Setup > Email > Email Account" + ), + exc=frappe.OutgoingEmailError, + ) comm.send_email( print_html=print_html, diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 73ab13b851..a89b9a2747 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -23,10 +23,6 @@ from frappe.utils.error import raise_error_on_no_output from frappe.utils.jinja import render_template from frappe.utils.user import get_system_managers -OUTGOING_EMAIL_ACCOUNT_MISSING = _( - "Please setup default Email Account from Setup > Email > Email Account" -) - class SentEmailInInbox(Exception): pass @@ -319,7 +315,7 @@ class EmailAccount(Document): @classmethod @raise_error_on_no_output( keep_quiet=lambda: not cint(frappe.get_system_settings("setup_complete")), - error_message=OUTGOING_EMAIL_ACCOUNT_MISSING, + error_message=_("Please setup default Email Account from Setup > Email > Email Account"), error_type=frappe.OutgoingEmailError, ) # noqa @cache_email_account("outgoing_email_account") diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py index d621fd2bba..1211419de1 100644 --- a/frappe/email/smtp.py +++ b/frappe/email/smtp.py @@ -1,24 +1,12 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE import smtplib -import _socket - import frappe from frappe import _ from frappe.utils import cint, cstr -CONNECTION_FAILED = _("Could not connect to outgoing email server") -AUTH_ERROR_TITLE = _("Invalid Credentials") -AUTH_ERROR = _("Incorrect email or password. Please check your login credentials.") -SOCKET_ERROR_TITLE = _("Incorrect Configuration") -SOCKET_ERROR = _("Invalid Outgoing Mail Server or Port") -SEND_MAIL_FAILED = _("Unable to send emails at this time") -EMAIL_ACCOUNT_MISSING = _( - "Email Account not setup. Please create a new Email Account from Setup > Email > Email Account" -) - class InvalidEmailCredentials(frappe.ValidationError): pass @@ -65,7 +53,12 @@ class SMTPServer: self._session = None if not self.server: - frappe.msgprint(EMAIL_ACCOUNT_MISSING, raise_exception=frappe.OutgoingEmailError) + frappe.msgprint( + _( + "Email Account not setup. Please create a new Email Account from Setup > Email > Email Account" + ), + raise_exception=frappe.OutgoingEmailError, + ) @property def port(self): @@ -93,7 +86,9 @@ class SMTPServer: try: _session = SMTP(self.server, self.port) if not _session: - frappe.msgprint(CONNECTION_FAILED, raise_exception=frappe.OutgoingEmailError) + frappe.msgprint( + _("Could not connect to outgoing email server"), raise_exception=frappe.OutgoingEmailError + ) self.secure_session(_session) if self.login and self.password: @@ -106,16 +101,12 @@ class SMTPServer: self._session = _session return self._session - except smtplib.SMTPAuthenticationError as e: + except smtplib.SMTPAuthenticationError: self.throw_invalid_credentials_exception() - except _socket.error as e: + except OSError: # Invalid mail server -- due to refusing connection - frappe.throw(SOCKET_ERROR, title=SOCKET_ERROR_TITLE) - - except smtplib.SMTPException: - frappe.msgprint(SEND_MAIL_FAILED) - raise + frappe.throw(_("Invalid Outgoing Mail Server or Port"), title=_("Incorrect Configuration")) def is_session_active(self): if self._session: @@ -130,4 +121,8 @@ class SMTPServer: @classmethod def throw_invalid_credentials_exception(cls): - frappe.throw(AUTH_ERROR, title=AUTH_ERROR_TITLE, exc=InvalidEmailCredentials) + frappe.throw( + _("Incorrect email or password. Please check your login credentials."), + title=_("Invalid Credentials"), + exc=InvalidEmailCredentials, + )