From 73493976e88305a129dedcab35f1be9f19901dbd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 26 Jun 2019 08:41:36 +0530 Subject: [PATCH] Sending email: Fix an error on some windows machines with non-ASCII hostnames --- src/calibre/utils/smtp.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/calibre/utils/smtp.py b/src/calibre/utils/smtp.py index d988c59b9e..12d49d8f48 100644 --- a/src/calibre/utils/smtp.py +++ b/src/calibre/utils/smtp.py @@ -11,21 +11,31 @@ This module implements a simple commandline SMTP client that supports: ''' import sys, traceback, os, socket, encodings.idna as idna -from calibre import isbytestring, force_unicode -from calibre.constants import ispy3 +from calibre import isbytestring +from calibre.constants import ispy3, iswindows from polyglot.builtins import unicode_type +def decode_fqdn(fqdn): + if isinstance(fqdn, bytes): + enc = 'mbcs' if iswindows else 'utf-8' + try: + fqdn = fqdn.decode(enc) + except Exception: + fqdn = '' + return fqdn + + def safe_localhost(): # RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and # if that can't be calculated, that we should use a domain literal # instead (essentially an encoded IP address like [A.B.C.D]). - fqdn = socket.getfqdn() + fqdn = decode_fqdn(socket.getfqdn()) if '.' in fqdn and fqdn != '.': # Some mail servers have problems with non-ascii local hostnames, see # https://bugs.launchpad.net/bugs/1256549 try: - local_hostname = idna.ToASCII(force_unicode(fqdn)) + local_hostname = idna.ToASCII(fqdn) except Exception: local_hostname = 'localhost.localdomain' else: