通过Python 2.6发送电子邮件(Gmail)时出错 [英] Error Sending Email (Gmail) Via Python 2.6

查看:983
本文介绍了通过Python 2.6发送电子邮件(Gmail)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个一直让我困惑了一会儿。任何人都可以看到我会错了吗?这个代码在Python 2.7中工作得很好;

This one has been baffling me for a while. Can anyone see where i'm going wrong? This code works fine in Python 2.7; however, it breaks when running through a cron (Python 2.6).

def send_email (message, status):
    fromaddr = 'sam@gmail.com'
    toaddrs = 'sam@gmail.com'
    server = SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('username-example', 'password-example')
    server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message))
    server.quit()

send_email('message text','subject text')

通过cron会产生以下错误:

Through the cron it produces this error:

Traceback (most recent call last):
  File "/home/user/weather-script.py", line 30, in <module>
    send_email('message text', 'subject text')
  File "/home/user/weather-script.py", line 11, in send_email
    server.login('username-example', 'password-example')
  File "/usr/lib64/python2.6/smtplib.py", line 589, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com    /ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsZn\n5.7.14 KZ5OF6iSxSCasonEce6H27TM-l4ithBaTtxpg8GbcEzJ522-_MUlYZJWIbc-ZwVnuslJOQ\n5.7.14 _Fu7bpO9-xOfqDi-eiSAPRw8_QLth-Z9ytfeWYIJi0Ez8F_p5joplfR7IoXw4V8VisI7pq\n5.7.14 8NTPoVFqvUEldEI5wL8AukhoPpVfDiX25557ky_W7N6UZLb3efGuvnbhrBsmg5gvlzj1DG\n5.7.14 1GchFIA> Please log in via your web browser and then try again.\n5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787\n5.7.14 54 xv2sm104667531pbb.39 - gsmtp')

感谢您的时间。任何帮助将非常感激。

Thanks for your time. Any help will be greatly appreciated.

Sam。

推荐答案

最终得到它的工作。原来,我只需要添加几行:

I've finally got it to work. It turns out I just needed to add a couple of extra lines:

server.ehlo()
server.starttls()
server.ehlo()

因此,最终脚本为:

def send_email (message, status):
    fromaddr = 'sam@gmail.com'
    toaddrs = 'sam@gmail.com'
    server = SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login('username-example', 'password-example')
    server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message))
    server.quit()

send_email('message text','subject text')

这篇关于通过Python 2.6发送电子邮件(Gmail)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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