python – 代表Google Apps用户发送电子邮件

根据文档,如果该用户拥有Gmail或Google Apps帐户,则可以代表当前登录的用户使用GAE发送电子邮件:

For security purposes, the sender address of a message must be the
email address of an administrator for the application or any valid
email receiving address for the app (see Receiving Mail). The sender
can also be the Google Account email address of the current user who
is signed in, if the user’s account is a Gmail account or is on a
domain managed by Google Apps.

以下代码适用于代表Gmail用户发送电子邮件,但不适用于Google Apps用户.尝试从Google Apps用户发送邮件会导致“未经授权的发件人”错误.

current_user = users.get_current_user()
message = mail.EmailMessage()
message.sender = current_user.email()
message.subject = 'subject text'
message.to = 'joe@example.com'
message.body = 'body text'
if message.is_initialized():
    try:
        message.send()
    except Exception, e:
        logging.error('Unable to send email update: %s' % e)
else:
    logging.error('Email message improperly initialized')

我错过了什么?我应该注意其他依赖项吗?

编辑:

完整的堆栈跟踪:

Unauthorized sender
Traceback (most recent call last):
  File "/base/data/home/apps/s~core-comps/1.358275951854397525/handler_cs_ticket.py", line 274, in sendEmailCopyToClient
    message.send()
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py", line 900, in send
    raise ERROR_MAP[e.application_error](e.error_detail)
InvalidSenderError: Unauthorized sender

最佳答案 看起来问题是您的应用程序正在使用联合登录,这是一项实验性功能,不适用于代表Google Apps帐户发送.您可以在管理控制台的“应用程序设置”页面上更改此设置.

我将这个添加到文档中.

点赞