所以我可以使用smtplib(使用下面的脚本)登录到gmail并通过gmail发送邮件,但我想知道使用oauth2是否是一个类似imaplib的选项?我在smtplib文档页面上没有看到关于oauth的任何内容,也没有在google上找到任何东西。谢谢。
#! /usr/bin/python
import smtplib
to = 'myemailaddress'
gmail_user = 'myemailaddress'
gmail_pwd = 'passwd'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from me \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close();
编辑:
感谢萨米·维拉在他的回答中提供了非常详细的解释。不过,我有点麻烦。这是我的剧本:
#! /usr/bin/python
import oauth2 as oauth
import oauth2.clients.smtp as smtplib
consumer = oauth.Consumer('anonymous', 'anonymous')
token = oauth.Token('1/MI6B2DqJP4FEkDRLUKrD5l46sQ0758-2ucEKBY-DeB0', 'NysqNqVTulFsdHpSRrPP56sF')
url = "https://mail.google.com/mail/b/testing.oauth.1@gmail.com/smtp/"
conn = smtplib.SMTP('smtp.googlemail.com', 587)
conn.set_debuglevel(True)
conn.ehlo('test')
conn.starttls()
conn.authenticate(url, consumer, token)
header = 'To:testing.oauth.1@gmail.com\n' + 'From: testing.oauth.1@gmail.com\n' + 'Subject:testing \n'
msg = header + '\n this is test msg from me \n\n'
conn.sendmail('testing.oauth.1@gmail.com', 'testing.oauth.1@gmail.com', msg)
让我不解的是,它似乎可以验证:
send: 'ehlo test\r\n'
reply: '250-mx.google.com at your service, [75.173.8.127]\r\n'
reply: '250-SIZE 35882577\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [75.173.8.127]
SIZE 35882577
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
send: 'STARTTLS\r\n'
reply: '220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: 2.0.0 Ready to start TLS
send: 'AUTH XOAUTH R0VUIGh0dHBzOi8vbWFpbC5nb29nbGUuY29tL21haWwvYi90ZXN0aW5nLm9hdXRoLjFAZ21haWwuY29tL3NtdHAvIG9hdXRoX2JvZHlfaGFzaD0iMmptajdsNXJTdzB5VmIlMkZ2bFdBWWtLJTJGWUJ3ayUzRCIsb2F1dGhfY29uc3VtZXJfa2V5PSJhbm9ueW1vdXMiLG9hdXRoX25vbmNlPSI3Nzc0ODMyIixvYXV0aF9zaWduYXR1cmU9IkxuckZHODdxdHRxZUhsUlQ1emRndmtEZ1UzTSUzRCIsb2F1dGhfc2lnbmF0dXJlX21ldGhvZD0iSE1BQy1TSEExIixvYXV0aF90aW1lc3RhbXA9IjEzNDIxNDI3NzIiLG9hdXRoX3Rva2VuPSIxJTJGTUk2QjJEcUpQNEZFa0RSTFVLckQ1bDQ2c1EwNzU4LTJ1Y0VLQlktRGVCMCIsb2F1dGhfdmVyc2lvbj0iMS4wIg==\r\n'
reply: '235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: 2.7.0 Accepted
但在发送电子邮件时,似乎忘记了conn
已经过身份验证:
send: 'ehlo [192.168.2.4]\r\n'
reply: '250-mx.google.com at your service, [75.173.8.127]\r\n'
reply: '250-SIZE 35882577\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-AUTH LOGIN PLAIN XOAUTH\r\n'
reply: '250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [75.173.8.127]
SIZE 35882577
8BITMIME
AUTH LOGIN PLAIN XOAUTH
ENHANCEDSTATUSCODES
send: 'mail FROM:<testing.oauth.1@gmail.com> size=107\r\n'
reply: '530-5.5.1 Authentication Required. Learn more at\r\n'
reply: '530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tu7sm3163839pbc.55\r\n'
reply: retcode (530); Msg: 5.5.1 Authentication Required. Learn more at
5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tu7sm3163839pbc.55
send: 'rset\r\n'
reply: '250 2.1.5 Flushed tu7sm3163839pbc.55\r\n'
reply: retcode (250); Msg: 2.1.5 Flushed tu7sm3163839pbc.55
Traceback (most recent call last):
File "./gmail_send3.py", line 47, in <module>
conn.sendmail('testing.oauth.1@gmail.com', 'testing.oauth.1@gmail.com', msg)
File "/usr/lib/python2.7/smtplib.py", line 713, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tu7sm3163839pbc.55', 'testing.oauth.1@gmail.com')
有趣的是,我认为这是可能的,我发现了以下链接
https://developers.google.com/google-apps/gmail/oauth_overview
更新
https://github.com/google/gmail-oauth2-tools/wiki/OAuth2DotPyRunThrough
这是使用oauth2的新演练,似乎xoauth已被声明为过时,我已更新URL以获取较旧版本的
xoauth.py
文件,以防仍有人需要它,尽管看起来Google已更改了它们的API并且此演练不再适用。您可以直接使用xoauth或https://github.com/simplegeo/python-oauth2/来进行实际的通信,我推荐后者,它更通用。
显然,您首先需要下载
xoauth.py
脚本。让我们测试一下:
看来我们可以走了。 您可能需要设置一个
virtualenv
从文件中取权:
对于smtp:
发送电子邮件:
在发送电子邮件之前,请确保您进行了身份验证。
希望这有帮助。。。
更新
可能有一个bug需要重新验证,下面应该可以工作。
尽管您也可以直接使用
xoauth
:这似乎总是有效的,我应该向
python-oauth2
团队提交一张罚单,谢谢。但这似乎也总是有效的
在进行身份验证之前,您需要调用
conn.ehlo()
。相关问题 更多 >
编程相关推荐