This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: SMTPHandler in the logging module fails with unicode strings
Type: Stage: resolved
Components: Versions: Python 3.6, Python 3.4, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: python-dev, r.david.murray, simon04, vinay.sajip
Priority: normal Keywords: patch

Created on 2015-10-15 13:02 by simon04, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
SMTPHandler-unicode-v1.patch simon04, 2015-10-15 13:02 review
Messages (4)
msg253043 - (view) Author: Simon04 (simon04) * Date: 2015-10-15 13:02
This relates to the unresolved issue9208 (Python 2).

SMTPHandler fails when receiving unicode strings.

Example (from msg109621):
import logging,logging.handlers
smtpHandler = logging.handlers.SMTPHandler(
    fromaddr="", toaddrs="",
    subject=u"error message")
LOG = logging.getLogger()

… fails:
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/", line 985, in emit
    smtp.sendmail(self.fromaddr, self.toaddrs, msg)
  File "/usr/lib/python3.5/", 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/", line 8, in <module>
Message: 'accentué'
Arguments: ()

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

Patch attached.
msg253045 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-10-15 13:51
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...)
msg253127 - (view) Author: Simon04 (simon04) * Date: 2015-10-17 13:06
I omitted the date header w/o intent. Basically because I couldn't quickly figure out how to set it.
msg253131 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-10-17 15:24
New changeset eb843115e052 by Vinay Sajip in branch '3.4':
Closes #25411: Improved Unicode support in SMTPHandler.

New changeset b99b3ddd0ac4 by Vinay Sajip in branch '3.5':
Closes #25411: Merged fix from 3.4.

New changeset 522b5cdffd42 by Vinay Sajip in branch 'default':
Closes #25411: Merged fix from 3.5.
Date User Action Args
2022-04-11 14:58:22adminsetgithub: 69597
2015-10-17 15:24:37python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg253131

resolution: fixed
stage: resolved
2015-10-17 13:06:25simon04setmessages: + msg253127
2015-10-15 13:51:45r.david.murraysetnosy: + vinay.sajip
messages: + msg253045
2015-10-15 13:21:54simon04setnosy: + r.david.murray
2015-10-15 13:02:06simon04create