【问题标题】:Auth error sending email with smtp and Gmail in Google App Engine在 Google App Engine 中使用 smtp 和 Gmail 发送电子邮件的身份验证错误
【发布时间】:2014-05-15 14:00:23
【问题描述】:

我正在尝试通过 App Engine 中的 SMTP 使用我的 Google Apps 电子邮件发送电子邮件,但在sendmail 通话期间我收到“需要授权”。 auth 调用本身似乎成功了。

这是我要发送的消息。这是通过 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 <ela@mydomain.com>
Date: Thu, 3 Apr 2014 15:12:26 +0800
Message-ID: <CAN8SGD-joz9Adq9QpKowME3N0xmgwegYGYwRVQh_9kbGfTa75A@mail.gmail.com>
Subject: =?UTF-8?B?RGVsYXllZCBlbWFpbCB0ZXN0ICMyIMOlw5/iiJo=?=
From: John Del Rosario <john@mydomain.com>
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' % ('john@mydomain.com', 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('john@mydomain.com', ['ela@mydomain.com'], 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:<john@mydomain.com> 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'john@mydomain.com')

【问题讨论】:

  • 在过去的六年里,您是否能够发送电子邮件?解决方案是什么?

标签: python google-app-engine smtp gmail


【解决方案1】:

您应该通过电子邮件注册 Google App Engine 应用。谷歌对来自电子邮件 ID 的限制。这是工作代码。

__author__ = 'Om Narayan'
__revision__ = '$'
__version__ = '0.1'
__maintainer__ = 'Om Narayan'
__email__ = 'om.narayan29@gmail.com'
__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()

【讨论】:

  • 那是使用 AppEngine 的mail api。我正在尝试做的事情是不可能的。我需要通过 Gmail 的 SMTP 发送。
  • 在他发送发件人地址之前发生错误。
猜你喜欢
  • 2013-11-24
  • 2015-07-08
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 2014-12-05
  • 2011-01-17
  • 1970-01-01
  • 2015-11-23
相关资源
最近更新 更多