没有使用 smtplib localhost 发送电子邮件
no email sent with smtplib localhost
我正在使用以下代码发送电子邮件
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 实例。
发生这种情况是因为 aws 不允许从 'localhost' 发送电子邮件。您需要使用特定的 AWS 电子邮件服务器。有关 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html 的更多信息。
这样做大概是为了限制使用从 AWS 服务器发送的非法电子邮件。
我正在使用以下代码发送电子邮件
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 实例。
发生这种情况是因为 aws 不允许从 'localhost' 发送电子邮件。您需要使用特定的 AWS 电子邮件服务器。有关 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp.html 的更多信息。
这样做大概是为了限制使用从 AWS 服务器发送的非法电子邮件。