Sending email on behalf of Google Apps user

2020-04-08 01:27发布

According to the documentation it is possible to send an email message using GAE on behalf of the currently logged in user, if that user has a Gmail or Google Apps account:

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.

The following code works for sending emails on behalf of Gmail users but not Google Apps users. Attempting to send mail from a Google Apps user results in an 'Unauthorized sender' error.

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')

What am I missing? Are there other dependencies that I should be aware of?

EDIT:

Full stacktrace:

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

1条回答
神经病院院长
2楼-- · 2020-04-08 01:57

It looks like the problem is that your application is using Federated Login, which is an experimental feature and doesn't work with sending on behalf of Google Apps accounts. You can change this on the "Application Settings" page in the admin console.

I'll add this to the documentation.

查看更多
登录 后发表回答