self.trace('Could not save sent message') # not a show-stopper
def encodeHeader(self, headertext, unicodeencoding='utf-8'):
"""
4E: encode composed non-ascii message headers content per both email
and Unicode standards, according to an optional user setting or UTF-8;
header.encode adds line breaks in header string automatically if needed;
"""
try:
headertext.encode('ascii')
except:
try:
hdrobj = email.header.make_header([(headertext, unicodeencoding)])
headertext = hdrobj.encode()
except:
pass # auto splits into multiple cont lines if needed
return headertext # smtplib may fail if it won't encode to ascii
def encodeAddrHeader(self, headertext, unicodeencoding='utf-8'):
"""
4E: try to encode non-ASCII names in email addresess per email, MIME,
and Unicode standards; if this fails drop name and use just addr part;
if cannot even get addresses, try to decode as a whole, else smtplib
may run into errors when it tries to encode the entire mail as ASCII;
utf-8 default should work for most, as it formats code points broadly;
inserts newlines if too long or hdr.encode split names to multiple lines,
but this may not catch some lines longer than the cutoff (improve me);
as used, Message.as_string formatter won't try to break lines further;
see also decodeAddrHeader in mailParser module for the inverse of this;
"""
try:
pairs = email.utils.getaddresses([headertext]) # split addrs + parts
encoded = []
for name, addr in pairs:
try:
name.encode('ascii') # use as is if okay as ascii
except UnicodeError: # else try to encode name part
try:
uni = name.encode(unicodeencoding)
hdr = email.header.make_header([(uni, unicodeencoding)])
name = hdr.encode()
except:
name = None # drop name, use address part only
joined = email.utils.formataddr((name, addr)) # quote name if need
encoded.append(joined)
fullhdr = ', '.join(encoded)
if len(fullhdr) > 72 or '\n' in fullhdr: # not one short line?
fullhdr = ',\n '.join(encoded) # try multiple lines
return fullhdr
except:
return self.encodeHeader(headertext)
def authenticateServer(self, server):
The mailtools Utility Package | 965