As you know, python smtplib has a debug level.When I set it a true param, it will print some send information.
The problem is, I try to get the debug info to log into a file, but They just stay on my cmd console.
How can I do to log them?
Info like this:
connect: ('192.168.1.101', 25)
connect: (25, '192.168.1.101')
reply: '220 ESMTP on WinWebMail [3.8.1.3] ready. http://www.winwebmail.com\r\n'
reply: retcode (220); Msg: ESMTP on WinWebMail [3.8.1.3] ready. http://www.winw
ebmail.com
connect: ESMTP on WinWebMail [3.8.1.3] ready. http://www.winwebmail.com
send: 'ehlo [169.254.63.67]\r\n'
reply: '250-SIZE\r\n'
reply: '250 AUTH LOGIN\r\n'
reply: retcode (250); Msg: SIZE
AUTH LOGIN
bla bla bla bla........
The
smtplibprints directly tostderr, e.g. line 823 in smtplib.py:You’d have to either monkey patch sys.stderr before you import
smtpliborsmtplib.stderrbefore you run your mail code.I might also suggest patching
smtplib.stderrwith a custom object that has awritemethod to wrap your logging code (if you are using the logging library for instance):This question contains some useful examples of patching stderr with context managers.