2014-04-03 40 views
0

我試圖通過App Engine中的SMTP通過Google Apps電子郵件發送電子郵件,但在sendmail調用期間我收到了「需要授權」。看起來auth調用本身成功了。通過Google App Engine中的smtp和Gmail發送電子郵件的身份驗證錯誤

這是我要發送的消息。這是通過IMAP檢索的草稿,所以我知道訪問令牌是好的。它是由Python的email.Message.as_string方法生成的。

MIME-Version: 1.0 
Received: by 10.76.124.8 with HTTP; Thu, 3 Apr 2014 00:12:26 -0700 (PDT) 
To: Ela Honoridez II <[email protected]> 
Date: Thu, 3 Apr 2014 15:12:26 +0800 
Message-ID: <[email protected]om> 
Subject: =?UTF-8?B?RGVsYXllZCBlbWFpbCB0ZXN0ICMyIMOlw5/iiJo=?= 
From: John Del Rosario <[email protected]> 
Content-Type: multipart/alternative; boundary=14dae94edfeda1125304f61e1fec 

--14dae94edfeda1125304f61e1fec 
Content-Type: text/plain; charset=UTF-8 

foo bar 

--14dae94edfeda1125304f61e1fec 
Content-Type: text/html; charset=UTF-8 

<div dir="ltr">foo bar</div> 

--14dae94edfeda1125304f61e1fec-- 

這裏是我的代碼:

auth_string = 'user=%s^Aauth=Bearer %s^A^A' % ('[email protected]', access_token) 
smtp = smtplib.SMTP('smtp.gmail.com', 587) 
smtp.set_debuglevel(True) 
smtp.ehlo() 
smtp.starttls() 
smtp.ehlo() 
smtp.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(auth_string)) 
smtp.sendmail('[email protected]', ['[email protected]'], rfc822_msg_str) 

這裏從SMTP的調試消息。

send: 'ehlo dev.myapp.appspot.com\r\n' 
reply: '250-mx.google.com at your service, [74.125.182.85]\r\n' 
reply: '250-SIZE 35882577\r\n' 
reply: '250-8BITMIME\r\n' 
reply: '250-STARTTLS\r\n' 
reply: '250-ENHANCEDSTATUSCODES\r\n' 
reply: '250 CHUNKING\r\n' 
reply: retcode (250); Msg: mx.google.com at your service, [74.125.182.85] 
SIZE 35882577 
8BITMIME 
STARTTLS 
ENHANCEDSTATUSCODES 
CHUNKING 
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: 'ehlo dev.myapp.appspot.com\r\n' 
reply: '250-mx.google.com at your service, [74.125.182.85]\r\n' 
reply: '250-SIZE 35882577\r\n' 
reply: '250-8BITMIME\r\n' 
reply: '250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN\r\n' 
reply: '250-ENHANCEDSTATUSCODES\r\n' 
reply: '250 CHUNKING\r\n' 
reply: retcode (250); Msg: mx.google.com at your service, [74.125.182.85] 
SIZE 35882577 
8BITMIME 
AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 
ENHANCEDSTATUSCODES 
CHUNKING 
send: 'AUTH XOAUTH2 (snipped base64 encoded auth string)\r\n' 
reply: '501 5.5.2 Cannot Decode response s9sm12053561igw.16 - gsmtp\r\n' 
reply: retcode (501); Msg: 5.5.2 Cannot Decode response s9sm12053561igw.16 - gsmtp 
send: u'mail FROM:<[email protected]> size=651\r\n' 
reply: '530-5.5.1 Authentication Required. Learn more at\r\n' 
SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 s9sm12053561igw.16 - gsmtp', u'[email protected]') 

回答

0

您從電子郵件應該註冊Google App Engine應用程序。 Google對來自電子郵件ID的限制。這是工作代碼。

__author__ = 'Om Narayan' 
__revision__ = '$' 
__version__ = '0.1' 
__maintainer__ = 'Om Narayan' 
__email__ = '[email protected]' 
__status__ = 'Prototype' 

import json 
from app.baseRequestHandler import * 
from google.appengine.api import mail 
from webapp2_extras import auth, sessions, jinja2 


class Email(BaseRequestHandler): 
    def send(self, values, templateFile): 
    message = mail.EmailMessage(sender="<Sender Name> <sender email id registered with google app engine>", 
          subject=values['subject']) 
    message.to = "%s <%s>" % (values['userName'], values['userEmail']) 
    message.body = self.jinja2.render_template('/email/%s.txt' % templateFile, **values) 
    message.html = self.jinja2.render_template('/email/%s.html' % templateFile, **values) 
    message.send() 

    def sendMulti(self, values, templateFile): 
    message = mail.EmailMessage(
       sender="%s (<Sender Name>) <sender email id registered with google app engine>" % values["authorName"], 
       subject=values['subject']) 
    message.to = "%s" % (values['recipients']) 
    message.body = self.jinja2.render_template('/email/%s.txt' % templateFile, **values) 
    message.html = self.jinja2.render_template('/email/%s.html' % templateFile, **values) 
    message.send() 
+0

這是使用AppEngine的'mail' api。這是不可能的,我想要做的事情。我需要通過Gmail的SMTP發送它。 – john2x

相關問題