如何获取smtp邮件日志

How to get smtp email log

我想在向错误的电子邮件地址发送电子邮件时获得日志。

所以,我写了这个命令。

mailserver = mymailserver
to_email = emailaddress
from_email = fromaddress
subject = SBJECT
original_message = TEXT
message = MESSAGE

server = smtplib.SMTP(mailserver)
debug = server.set_debuglevel(1)
server.sendmail(from_mail,to_mail, message)
server.quit()
print(debug)

我只想知道连接状态日志。

我应该编辑哪个代码?

我试过这个脚本,但效果不佳。

server = smtplib.SMTP(mailserver)
mail_response = server.sendmail(from_mail,to_mail, message)
server.quit()
print(mail_response)

谢谢你帮助我。

试试这个并阅读 docs。希望对您有所帮助

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.
# Or simply skip this part
with open(textfile) as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()