4

我复制并粘贴了这个示例并填写了我自己的电子邮件地址。

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open('/Users/Jon/dev/iit/test-tools/logs/practice_tests.test_one.log', 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

me = 'Jon@MacBookPro.com'
you = 'jon.drowell@yahoo.com'

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of the log file'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost', 1025)
s.sendmail(me, [you], msg.as_string())
s.quit()

然后我打开另一个终端窗口并运行以下命令:

python -m smtpd -n -c DebuggingServer localhost:1025

当我运行该文件以发送电子邮件时,它会正确完成,并且运行该python -m smtpd -n -c DebuggingServer localhost:1025命令的终端会打印一条看起来正确的漂亮日志消息:

---------- MESSAGE FOLLOWS ----------
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: The contents of the log file
From: Jon@MacBookPro.com
To: jon.crowell@yahoo.com
X-Peer: 127.0.0.1

line one
line two
line three
------------ END MESSAGE ------------

但是当我检查我的雅虎电子邮件地址时,电子邮件永远不会到达。我等了大约5分钟,我认为这已经足够了。我做错什么了?

4

2 回答 2

5

来自smtpd 文档

class smtpd.DebuggingServer(localaddr, remoteaddr) 创建一个新的调试服务器。参数根据 SMTPServer。消息将被丢弃,并打印在标准输出上。

于 2013-01-17T05:09:52.773 回答
1

调试邮件消息并查看幕后情况的好方法,只需设置 smtp 对象的调试级别:

debuglevel = True
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mail.set_debuglevel(debuglevel)
mail.starttls()
mail.login(SMTP_USERNAME, SMTP_PASSWORD)
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
mail.quit()

这使您可以在此过程中查看步骤/检查:

send: 'ehlo localhost.localdomain\r\n'
reply: '250-smtp.mail.yahoo.com\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 41697280\r\n'
reply: '250-8 BITMIME\r\n'
reply: '250-AUTH PLAIN LOGIN XYMCOOKIE\r\n'
reply: '250 STARTTLS\r\n'
reply: retcode (250); Msg: smtp.mail.yahoo.com
PIPELINING
SIZE 41697280
8 BITMIME
AUTH PLAIN LOGIN XYMCOOKIE
STARTTLS
send: 'STARTTLS\r\n'
reply: '220 2.0.0 Start TLS\r\n'
reply: retcode (220); Msg: 2.0.0 Start TLS
send: 'ehlo localhost.localdomain\r\n'
reply: '250-smtp.mail.yahoo.com\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 41697280\r\n'
reply: '250-8 BITMIME\r\n'
reply: '250 AUTH PLAIN LOGIN XYMCOOKIE\r\n'
reply: retcode (250); Msg: smtp.mail.yahoo.com
PIPELINING
SIZE 41697280
8 BITMIME
AUTH PLAIN LOGIN XYMCOOKIE
send: 'AUTH PLAIN AGZlZHVzcaddfjbkejkjkVAeWFob28uY29tAG1taHR0ODAxQA==\r\n'
reply: '235 2.0.0 OK\r\n'
reply: retcode (235); Msg: 2.0.0 OK
send: 'mail FROM:<example@yahoo.com> size=471\r\n'
reply: '250 OK , completed\r\n'
reply: retcode (250); Msg: OK , completed
send: 'rcpt TO:<example@gmail.com>\r\n'
reply: '250 OK , completed\r\n'
reply: retcode (250); Msg: OK , completed
send: 'data\r\n'
reply: '354 Start Mail. End with CRLF.CRLF\r\n'
reply: retcode (354); Msg: Start Mail. End with CRLF.CRLF
data: (354, 'Start Mail. End with CRLF.CRLF')
send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nSubject: REMINDER:Company - Service at appointmentTime\r\nFrom: example@yahoo.com\r\nTo: example@gmail.com\r\n\r\n\r\nHello, [username]! Just wanted to send a friendly appointment\r\nreminder for your appointment:\r\n[Company]\r\nWhere: [companyAddress]\r\nTime: [appointmentTime]\r\n\r\nCompany URL: [companyUrl]\r\n\r\nChange appointment?? Add Service??\r\n\r\nchange notification preference (text msg/email)\r\n.\r\n'
reply: '250 OK , completed\r\n'
reply: retcode (250); Msg: OK , completed
data: (250, 'OK , completed')
send: 'quit\r\n'
reply: '221 Service Closing transmission\r\n'
reply: retcode (221); Msg: Service Closing transmission
于 2013-10-15T23:31:47.690 回答