我正在尝试编写一个 python 代码,它将错误消息发送到我的电子邮件帐户。我使用 smtplib 从 localhost 向我的 gmail 帐户发送消息。我正在尝试在 jupyter notebook 中关注:
import smtplib
SERVER = "localhost"
LOGIN = "sourabh@localhost.com"
PASSWORD = "sourabh"
FROM = "sourabh@localhost.com"
TO = ["sourabhjbp@gmail.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This message was sent with Python's smtplib."
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER,port=25)
server.set_debuglevel(1)
server.login(LOGIN, PASSWORD)
server.sendmail(FROM, TO, message)
server.quit()
它以消息“(221,'再见')”退出。以下是完整的调试日志,
send: 'ehlo [192.168.56.1]\r\n'
reply: '250-localhost\r\n'
reply: '250-SIZE 20480000\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250 HELP\r\n'
reply: retcode (250); Msg: localhost
SIZE 20480000
AUTH LOGIN PLAIN
HELP
send: 'AUTH PLAIN AHNvdXJhYmhAbG9jYWxob3N0LmNvbQBzb3VyYWJo\r\n'
reply: '235 authenticated.\r\n'
reply: retcode (235); Msg: authenticated.
send: 'mail FROM:<sourabh@localhost.com> size=115\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: OK
send: 'rcpt TO:<sourabhjbp@gmail.com>\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: OK
send: 'data\r\n'
reply: '354 OK, send.\r\n'
reply: retcode (354); Msg: OK, send.
data: (354, 'OK, send.')
send: "From: sourabh@localhost.com\r\nTo: sourabhjbp@gmail.com\r\nSubject: Hello!\r\n\r\nThis message was sent with Python's smtplib.\r\n.\r\n"
reply: '250 Queued (0.032 seconds)\r\n'
reply: retcode (250); Msg: Queued (0.032 seconds)
data: (250, 'Queued (0.032 seconds)')
send: 'quit\r\n'
reply: '221 goodbye\r\n'
reply: retcode (221); Msg: goodbye