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.

classification
Title: logging.handlers.SMTPHandler
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eamanu, lidayan, matrixise, mdk, vinay.sajip
Priority: normal Keywords:

Created on 2019-02-14 08:45 by lidayan, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11854 closed lidayan, 2019-02-14 10:22
PR 11856 closed lidayan, 2019-02-14 11:17
Messages (16)
msg335511 - (view) Author: lidayan (lidayan) * Date: 2019-02-14 08:45
SSL encrypted socket on SMTPHandler error

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 1008, in emit
    smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 391, in getreply
    + str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out
msg335512 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-14 08:46
You have a timeout, maybe you have an issue with your SMTP server. But in this case, it's not an issue with Python.
msg335520 - (view) Author: Emmanuel Arias (eamanu) * Date: 2019-02-14 11:51
hmmm do you have some code to reproduce it?
msg335522 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-14 12:10
I re-open this issue because after the change of the subject, maybe there is an issue with the code of logging.handler.SMTPHandler + TLS.
msg335523 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-14 12:20
@lidayan

I have checked the code of your PR, and I think there is an issue with your code.

1. When you specify the secure flag to the SMTPHandler, it's because you will start the TLS connection if the SMTP server supports the STARTTLS. Firstly in plaintext mode and if STARTTLS is supported then we move to the TLS encryption.

2. If you use SSL + SMTP, it's not the same behavior because you support that your SMTP server will support a socket + ssl.

For my part, I suggest you the creation of a SMTPSSLHandler and use the SMTP_SSL class.
msg335524 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-14 12:26
Here is my suggestion

class SMTPSSLHander(SMTPHandler):
    def emit(self, record):
        """
        Emit a record.

        Format the record and send it to the specified addressees.
        """
        try:
            import smtplib
            from email.message import EmailMessage
            import email.utils

            port = self.mailport
            if not port:
                port = smtplib.SMTP_SSL_PORT
            keyfile = self.secure[0] if len(self.secure) > 0 else None
            certfile = self.secure[1] if len(self.secure) > 1 else None
            smtp = smtplib.SMTP_SSL(self.mailhost, port, timeout=self.timeout,
                                    keyfile=keyfile, certfile=certfile)
            if self.username:
                smtp.login(self.username, self.password)
            msg = EmailMessage()
            msg['From'] = self.fromaddr
            msg['To'] = ','.join(self.toaddrs)
            msg['Subject'] = self.getSubject(record)
            msg['Date'] = email.utils.localtime()
            msg.set_content(self.format(record))
            smtp.send_message(msg)
            smtp.quit()
        except Exception:
            self.handleError(record)
msg335525 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-14 12:28
I propose to create a SMTPSSLHandler class and submit a PR.
msg335527 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-02-14 12:35
lidayan If I understand correctly, you're trying to connect to an SMTP server that does *not* support STARTTLS, only implicit TLS on port 465?
msg335528 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-02-14 12:43
@lidayan, if you're trying implicit TLS, have you tried giving a username an an empty tuple to the secure parameter?

It looks like the empty tuple means "implicit TLS", see the docstring https://github.com/python/cpython/blob/7fea5ad9f081247927bc851605f3a6269cbcd84d/Lib/logging/handlers.py#L959
msg335531 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-02-14 13:28
lidayan (Ignore my latest comment I was reading your implementation, not the actual one...).
msg336084 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-20 13:04
@lidayan

I think it's a misuse of the logging.handlers.SMTPHandler class.
msg336173 - (view) Author: lidayan (lidayan) * Date: 2019-02-21 02:08
OK, I got it. 

> 在 2019年2月14日,16:46,Stéphane Wirtel <report@bugs.python.org> 写道:
> 
> 
> Stéphane Wirtel <stephane@wirtel.be> added the comment:
> 
> You have a timeout, maybe you have an issue with your SMTP server. But in this case, it's not an issue with Python.
> 
> ----------
> nosy: +matrixise
> resolution:  -> rejected
> stage:  -> resolved
> status: open -> closed
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue35995>
> _______________________________________
>
msg336178 - (view) Author: lidayan (lidayan) * Date: 2019-02-21 03:04
And I have changed the code, please help to review, thanks

> 在 2019年2月21日,10:08,李大炎 <840286247@qq.com> 写道:
> 
> OK, I got it. 
> 
> 
>> 在 2019年2月14日,16:46,Stéphane Wirtel <report@bugs.python.org> 写道:
>> 
>> 
>> Stéphane Wirtel <stephane@wirtel.be> added the comment:
>> 
>> You have a timeout, maybe you have an issue with your SMTP server. But in this case, it's not an issue with Python.
>> 
>> ----------
>> nosy: +matrixise
>> resolution:  -> rejected
>> stage:  -> resolved
>> status: open -> closed
>> 
>> _______________________________________
>> Python tracker <report@bugs.python.org>
>> <https://bugs.python.org/issue35995>
>> _______________________________________
>> 
>
msg336179 - (view) Author: lidayan (lidayan) * Date: 2019-02-21 03:20
1. When specify the secure flag to the SMTPHandler, means start TLS connection. 
2. if SMTP Server not support plaintext mode, but firstly in plaintext mode it is a error
msg336242 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2019-02-21 17:35
The existing implementation supports doing an SSL handshake using STARTTLS, which provides encryption for the actual email traffic. You are asking, it seems, to support a server that only listens on an already encrypted connection, and doesn't use STARTTLS. That would, in my book, be an *enhancement request* and not a bug. Your PR has removed the STARTTLS support - what is supposed to happen when connecting to a server that listens unencrypted and expects to use STARTTLS to initiate encypted traffic?
msg346054 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2019-06-19 14:47
Closing, as no answer has been received to the last question.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80176
2019-06-19 14:47:45vinay.sajipsetstatus: open -> closed
resolution: not a bug
messages: + msg346054
2019-02-21 17:35:15vinay.sajipsetmessages: + msg336242
2019-02-21 03:20:43lidayansetmessages: + msg336179
2019-02-21 03:04:15lidayansetmessages: + msg336178
2019-02-21 02:08:56lidayansetmessages: + msg336173
title: use SSL encrypted socket on logging.handlers.SMTPHandler -> logging.handlers.SMTPHandler
2019-02-20 13:04:54matrixisesetmessages: + msg336084
2019-02-14 13:28:18mdksetmessages: + msg335531
2019-02-14 12:43:20mdksetmessages: + msg335528
2019-02-14 12:35:39mdksetnosy: + mdk
messages: + msg335527
2019-02-14 12:28:04matrixisesetmessages: + msg335525
2019-02-14 12:26:24matrixisesetmessages: + msg335524
2019-02-14 12:22:44xtreaksetnosy: + vinay.sajip
2019-02-14 12:20:27matrixisesetmessages: + msg335523
2019-02-14 12:10:24matrixisesetstatus: closed -> open
resolution: rejected -> (no value)
messages: + msg335522
2019-02-14 11:51:09eamanusetnosy: + eamanu
messages: + msg335520
2019-02-14 11:17:21lidayansetpull_requests: + pull_request11888
2019-02-14 10:22:44lidayansetpull_requests: + pull_request11886
2019-02-14 08:47:46lidayansettitle: logging.handlers.SMTPHandler -> use SSL encrypted socket on logging.handlers.SMTPHandler
2019-02-14 08:46:58matrixisesetstatus: open -> closed

nosy: + matrixise
messages: + msg335512

resolution: rejected
stage: resolved
2019-02-14 08:45:46lidayancreate