From 2c4863d7e2e9bba6ef0918584dcde34a8edb6798 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 26 Apr 2009 15:53:52 -0700 Subject: [PATCH] Fix #2351 (Cannot email when using SSL SMTP server with login) --- src/calibre/utils/smtp.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/calibre/utils/smtp.py b/src/calibre/utils/smtp.py index 0234e27c55..87019ed146 100644 --- a/src/calibre/utils/smtp.py +++ b/src/calibre/utils/smtp.py @@ -81,7 +81,12 @@ def sendmail(msg, from_, to, localhost=None, verbose=0, timeout=30, for x in to: return sendmail_direct(from_, x, msg, timeout, localhost, verbose) import smtplib - cls = smtplib.SMTP if encryption == 'TLS' else smtplib.SMTP_SSL + class SMTP_SSL(smtplib.SMTP_SSL): # Workaround for bug in smtplib.py + def _get_socket(self, host, port, timeout): + smtplib.SMTP_SSL._get_socket(self, host, port, timeout) + return self.sock + + cls = smtplib.SMTP if encryption == 'TLS' else SMTP_SSL timeout = None # Non-blocking sockets sometimes don't work port = int(port) s = cls(timeout=timeout, local_hostname=localhost) @@ -93,6 +98,8 @@ def sendmail(msg, from_, to, localhost=None, verbose=0, timeout=30, s.starttls() s.ehlo() if username is not None and password is not None: + if encryption == 'SSL': + s.sock = s.file.sslobj s.login(username, password) s.sendmail(from_, to, msg) return s.quit()