无法使用 gmail 通过 python 发送电子邮件 - smtplib.SMTPException: 服务器不支持 SMTP AUTH 扩展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19765073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant send email via python using gmail - smtplib.SMTPException: SMTP AUTH extension not supported by server
提问by Tampa
I just want to send an email in python with an attachment
我只想用 python 发送一封带有附件的电子邮件
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.login('[email protected]','fu')
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
ATTACHMENTS = ['/tmp/2013-11-04-test.csv']
send_from=['[email protected]']
send_to=['[email protected]']
subject='adfadfadf'
text = 'adfadfadf'
send_mail(send_from, send_to, subject, text, files=ATTACHMENTS)
How do I auth? I have to provide a username and password. How?
我如何认证?我必须提供用户名和密码。如何?
Traceback (most recent call last):
File "/home/ubuntu/workspace/miliza-devops/classes/utilities.py", line 133, in <module>
send_mail(send_from, send_to, subject, text, files=ATTACHMENTS)
File "/home/ubuntu/workspace/miliza-devops/classes/utilities.py", line 124, in send_mail
smtp.login('[email protected]','fu')
File "/usr/lib/python2.7/smtplib.py", line 576, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
回答by Rob?
You need a call to starttls()
before you login:
您需要starttls()
在登录前致电:
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.starttls()
smtp.login('[email protected]', 'fu')
Also, your send_from
should be a str
, not a list
:
另外,您send_from
应该是 a str
,而不是 a list
:
send_from='[email protected]'
Note that smtp.starttls()
calls smtp.ehlo()
implicitly:
请注意,隐式smtp.starttls()
调用smtp.ehlo()
:
If there has been no previous EHLO or HELO command this session, this method tries ESMTP EHLO first. https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.starttls
如果之前没有 EHLO 或 HELO 命令此会话,则此方法首先尝试 ESMTP EHLO。https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.starttls
回答by Hack5
On gmail you also have to give a smtp.ehlo()
before smtp.starttls()
This is also duplicate of How to send an email with Gmail as provider using Python?
在 gmail 上,您还必须提供一个smtp.ehlo()
beforesmtp.starttls()
这也是如何使用 Python 以 Gmail 作为提供程序发送电子邮件的副本?