Skip to content

Commit

Permalink
Sending email: Fix an error on some windows machines with non-ASCII h…
Browse files Browse the repository at this point in the history
…ostnames
  • Loading branch information
kovidgoyal committed Jun 26, 2019
1 parent 1a7a1b2 commit 7349397
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/calibre/utils/smtp.py
Expand Up @@ -11,21 +11,31 @@
'''

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:
Expand Down

0 comments on commit 7349397

Please sign in to comment.