# 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(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
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.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
在s = smtplib.SMTP('localhost')
中,我是否必须设置一些东西才能工作..比如stmp服务器?
如果是的话我该怎么做?有人知道我是否可以使用 localhost 以外的 smtp 服务器吗? gmail smtp 服务器对我不起作用,因为它必须通过 gmail api 并且无法将电子邮件作为任意电子邮件发送
[编辑]
s = smtplib.SMTP('mail')
对我有用
有人知道stmp服务器'mail'
指的是什么吗?
最佳答案
回答您问题的核心:您确实需要一个 SMTP 服务器。您可以在本地设置,也可以使用远程服务器。
使用哪种 SMTP 服务以及如何设置服务器对于这个社区来说是一个太宽泛的问题 -- 您必须做一些研究然后来返回更具体的问题。
关于除 localhost 之外的 Python SMTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495411/