不能通过python使用gmail发送电子邮件 - smtplib.SMTPException:服务器不支持SMTP AUTH扩展 [英] Cant send email via python using gmail - smtplib.SMTPException: SMTP AUTH extension not supported by server

查看:3912
本文介绍了不能通过python使用gmail发送电子邮件 - smtplib.SMTPException:服务器不支持SMTP AUTH扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在python发送一封附件的电子邮件。

I just want to send an email in python with an attachment

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('fu@gmail.com','fu')
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()

ATTACHMENTS = ['/tmp/2013-11-04-test.csv']
send_from=['fu@gmail.com']
send_to=['fu@gmail.com']
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('fu@gmail.com','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.


推荐答案

您需要拨打 starttls()在登录之前:

You need a call to starttls() before you login:

smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.starttls()
smtp.login('fu@gmail.com', 'fu')

另外,您的 send_from 应该是一个 str ,而不是列表

Also, your send_from should be a str, not a list:

send_from='fu@gmail.com'

请注意, smtp.starttls()调用 smtp.ehlo()隐式:


如果没有以前的EHLO或HELO命令会话,此方法首先尝试ESMTP EHLO。 https://docs.python.org/2/library/smtplib .html#smtplib.SMTP.starttls

这篇关于不能通过python使用gmail发送电子邮件 - smtplib.SMTPException:服务器不支持SMTP AUTH扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆