From f4c7c7aeedb3976847505b0987d7f8af2850cd0a Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 25 Nov 2020 15:07:19 +0530 Subject: [PATCH 1/3] fix: set self.port instead of self.smtp_port --- frappe/email/smtp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py index f53b835757..391ce06c74 100644 --- a/frappe/email/smtp.py +++ b/frappe/email/smtp.py @@ -210,7 +210,7 @@ class SMTPServer: try: if self.use_ssl: if not self.port: - self.smtp_port = 465 + self.port = 465 self._sess = smtplib.SMTP_SSL((self.server or "").encode('utf-8'), cint(self.port) or None) From b3a8ecad632bf3f12bd588447fdb4acc97833c55 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 25 Nov 2020 17:34:44 +0530 Subject: [PATCH 2/3] refactor: don't encode server string --- frappe/email/smtp.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py index 391ce06c74..9ba81fa146 100644 --- a/frappe/email/smtp.py +++ b/frappe/email/smtp.py @@ -212,8 +212,7 @@ class SMTPServer: if not self.port: self.port = 465 - self._sess = smtplib.SMTP_SSL((self.server or "").encode('utf-8'), - cint(self.port) or None) + self._sess = smtplib.SMTP_SSL((self.server or ""), cint(self.port)) else: if self.use_tls and not self.port: self.port = 587 From 6d9a56e43cb2e80e74e781c93151b023e8256fd5 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 25 Nov 2020 18:51:50 +0530 Subject: [PATCH 3/3] feat: add tests --- frappe/email/test_smtp.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 frappe/email/test_smtp.py diff --git a/frappe/email/test_smtp.py b/frappe/email/test_smtp.py new file mode 100644 index 0000000000..869d708430 --- /dev/null +++ b/frappe/email/test_smtp.py @@ -0,0 +1,25 @@ +# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors +# License: The MIT License + +import unittest +from frappe.email.smtp import SMTPServer + +class TestSMTP(unittest.TestCase): + def test_smtp_ssl_session(self): + for port in [None, 0, 465, "465"]: + make_server(port, 1, 0) + + def test_smtp_tls_session(self): + for port in [None, 0, 587, "587"]: + make_server(port, 0, 1) + + +def make_server(port, ssl, tls): + server = SMTPServer( + server = "smtp.gmail.com", + port = port, + use_ssl = ssl, + use_tls = tls + ) + + server.sess \ No newline at end of file