Issue 25411: SMTPHandler in the logging module fails with unicode strings (original) (raw)

This relates to the unresolved (Python 2).

SMTPHandler fails when receiving unicode strings.

Example (from ): import logging,logging.handlers smtpHandler = logging.handlers.SMTPHandler( mailhost=("smtp.free.fr",25), fromaddr="from@free.fr", toaddrs="to@free.fr", subject=u"error message") LOG = logging.getLogger() LOG.addHandler(smtpHandler) LOG.error(u"accentu\u00E9")

… fails: --- Logging error --- Traceback (most recent call last): File "/usr/lib/python3.5/logging/handlers.py", line 985, in emit smtp.sendmail(self.fromaddr, self.toaddrs, msg) File "/usr/lib/python3.5/smtplib.py", line 846, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 108: ordinal not in range(128) Call stack: File "/tmp/x.py", line 8, in LOG.error(u"accentu\u00E9") Message: 'accentué' Arguments: ()

As discussed in and , EmailMessage/send_message should be used instead to resolve this issue.

Patch attached.

This mostly looks good to me, Vinay.

Simon: did you intentionally omit the date header, and if so why? (The smtp server normally adds one, but you can't really depend on that). Adding it would look like:

msg['Date'] = email.utils.localtime()

(Hmm. I wonder if send_message should add Date header if there isn't one...)