I wrote a simple program that sends email using smtplib. The program works perfectly and sends the email. Is there any option that I can view all the logs during the communication? Meaning is it possible to print or to view the smtp conversation that happened?
Asked
Active
Viewed 1,075 times
0
-
1Yes, sure, but we need more details about your project. First is: What logs? What kind of information you want? – piertoni Nov 04 '21 at 15:53
1 Answers
0
First you should check documentation for smtplib
- see set_debuglevel
with smtplib.SMTP_SSL(f'{host}:{port}') as server:
server.set_debuglevel(1)
server.login(login, password)
server.sendmail(sender_email, recipients, message.as_string())
For .set_debuglevel(1)
it displays
send: 'ehlo [127.0.1.1]\r\n'
reply: b'250-smtp.googlemail.com at your service, [79.163.228.253]\r\n'
reply: b'250-SIZE 35882577\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-CHUNKING\r\n'
reply: b'250 SMTPUTF8\r\n'
reply: retcode (250); Msg: b'smtp.googlemail.com at your service, [79.163.228.253]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8'
send: 'AUTH PLAIN ********\r\n'
reply: b'235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: b'2.7.0 Accepted'
For .set_debuglevel(2)
it adds timestamps
21:26:26.602350 send: 'ehlo [127.0.1.1]\r\n'
21:26:26.632756 reply: b'250-smtp.googlemail.com at your service, [79.163.228.253]\r\n'
21:26:26.632871 reply: b'250-SIZE 35882577\r\n'
21:26:26.632917 reply: b'250-8BITMIME\r\n'
21:26:26.632957 reply: b'250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\r\n'
21:26:26.633022 reply: b'250-ENHANCEDSTATUSCODES\r\n'
21:26:26.633072 reply: b'250-PIPELINING\r\n'
21:26:26.633112 reply: b'250-CHUNKING\r\n'
21:26:26.633151 reply: b'250 SMTPUTF8\r\n'
21:26:26.633201 reply: retcode (250); Msg: b'smtp.googlemail.com at your service, [79.163.228.253]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8'
It sends it on standard error
so if you want to get it in file 'output.txt'
then you would have to redirect sys.stderr
import sys
old_stderr = sys.stderr
sys.stderr = open('output.txt', 'w')
with smtplib.SMTP_SSL(f'{host}:{port}') as server:
server.set_debuglevel(1)
server.login(login, password)
server.sendmail(sender_email, recipients, message.as_string())
sys.stderr = old_stderr

furas
- 134,197
- 12
- 106
- 148
-
Some following question now about that Is it possible somehow to check the smtp logs conversationwithout sending any email? For example les say that I want to analyze my email and another mail server without sending any real messege to it? And between 2 randoms email? even if those emails adresses arent belong to me personaly – Omer Itach Nov 11 '21 at 16:19
-
on most systems there is program `telnet` to connect with `telnet server` but if you use it with `smtp server` (like `telnet smtp.gmail.com 465`) or `http server` (like `telnet stackoverflow.com 80`) then you can see messages from server, but first you have to manually send commands to server - and this is hard work (especially if server uses secure connection with `SSL`). The same way you can use `openssl` which uses `SSL`. See [Connecting to smtp.gmail.com via command line](https://stackoverflow.com/questions/1516754/connecting-to-smtp-gmail-com-via-command-line). – furas Nov 11 '21 at 16:58
-
instead of `telnet` you could write Python code using standard module `socket` but again you have to know what commands to send (so you have to know `SMTP` protocol). There should be more specialized programs for testing SMTP but you should search it rather on portals for [Security](https://security.stackexchange.com/) – furas Nov 11 '21 at 17:01
-
Hi I want to try and specify my question: I want to check if some of my company's emails are in the whitelist or the blacklist of mail servers of other organization. I thought to try to do that with python script using smtplib and check the logs, maybe analyze time differences (don't want send many emails because I don't want my domain to tag as spammer). Is there a way to do that without sending active emails? If I know the commands in smtp, so by using socket like you suggest? – Omer Itach Nov 29 '21 at 15:51
-
I think you can't check it without sending email. Server decides if filter email only when it get it. And you can't connect to other server and get information. If you try to use `smtp` to connect to other server and you try to use your email then it can say only that this mailbox doesn't exists on this server. It can't send mail from your local computer to this server. Your email has to go to your mailbox on your server, and this server can send to other server which puts it in other person mailbox. – furas Nov 29 '21 at 16:05