[英]Error Sending Email (Gmail) Via Python 2.6
这使我感到困惑了一段时间。 谁能看到我要去哪里错了? 这段代码在Python 2.7中可以正常工作; 但是,通过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会产生以下错误:
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')
谢谢你的时间。 任何帮助将不胜感激。
山姆。
我终于有了工作。 事实证明,我只需要添加一些额外的行:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.