没有通过smtplib localhost

时间:2018-07-30 20:16:42

标签: smtplib

我正在使用以下代码发送电子邮件

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

然后在控制台上从(https://docs.python.org/2/library/email-examples.html):

导入smtplib以获得实际的发送功能

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.

textfile = 'sample_email.txt'

fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

me = 'william@gmail.com'
you = 'william@mydomain.io'
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
SERVER = 'localhost'

s = smtplib.SMTP(SERVER, 1025)

s.sendmail(me, [you], msg.as_string())
s.quit()

我可以在终端上看到以下内容

---------- MESSAGE FOLLOWS ----------
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: The contents of sample_email.txt
From: william@gmail.com
To: william@mydomain.io
X-Peer: 127.0.0.1

this is a simple text message

------------ END MESSAGE ------------

,但是我在william@mydomain.io上没有收到电子邮件->垃圾邮件文件夹上也没有。

以防万一,我运行代码的服务器是一个AWS实例。

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为aws不允许从“本地主机”发送电子邮件。您需要使用特定的AWS电子邮件服务器。通过https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html获得更多信息。

大概是为了限制使用从AWS服务器发送的非法电子邮件。