python - Google AppEngine 无法将发送邮件作业推迟到默认任务队列,收到未经授权的发件人错误

标签 python google-app-engine sendmail task-queue

您好,我正在尝试通过 sendmail 延迟发送邮件。 能发邮件没问题。 我一尝试 deferred.defer(send_invitation,recipient),它就停止工作了。

我可以看到任务重试到最大允许限制,并且日志显示:

File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1136, in send
    raise ERROR_MAP[e.application_error](e.error_detail)
InvalidSenderError: Unauthorized sender

这是否意味着如果一个 sendmail 作业被发送到默认队列(或任何队列),它不再被认为是从原始发件人发送的,并且新的发件人(即队列)没有被授权? ?

不使用 deferred.defer 发送邮件,包括项目所有者的电子邮件和默认的 appengine 服务帐户。这两个帐户都有所需的权限/角色。

    import webapp2
    from google.appengine.ext import deferred
    import jinja2
    import os
    import datetime
    from google.appengine.api import users, mail


    recipient ='xxxxxx@xxxx.com'

    user = users.GetCurrentUser().email()

    print "THIS IS THE CURRENT USER %s" %user


    def send_invitation(recipient):
                print "In the def user email is %s" %user

                mail.send_mail(sender=user,to=recipient, subject='You\'re invited!',body='''You have been invited to join our community...''')



    class SendInvitationHandler(webapp2.RequestHandler):
        print "This is user within the class %s" %user
        def get(self):
            user = users.GetCurrentUser().email()
            tempalte_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.getcwd()))
            current_time =  datetime.datetime.now()
            template = tempalte_env.get_template('invitation.html')
            message = "<p>%s, and an invitation to %s has been deferred from %s</p>" % (datetime.datetime.now(),recipient, user)
            context = {'recipient': recipient,
                        'message': message,
                        'current_time': current_time,
                        'user': user
                        }
            self.response.out.write(template.render(context))

            deferred.defer(send_invitation,recipient)

app = webapp2.WSGIApplication([('/sendinvite', SendInvitationHandler)], debug=True)

deferred.defer(send_invitation,recipient) 行更改为 send_invitation(recipient) 并且它有效......

最佳答案

延迟任务在不同的处理程序上执行以响应不同的请求,它不再具有原始请求的上下文。所以基本上你不再有 user 集。

您需要收集在原始上下文中发送消息所需的所有信息,并将其作为参数传递给延迟任务。在您的特定情况下,除了 recipient 之外,您还需要传递 user,如下所示:

def send_invitation(recipient, sender):

    mail.send_mail(sender=sender, to=recipient, 
                   subject='You\'re invited!',
                   body='''You have been invited to join our community...''')

并延迟它:

        deferred.defer(send_invitation, recipient, user)

关于python - Google AppEngine 无法将发送邮件作业推迟到默认任务队列,收到未经授权的发件人错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46002825/

相关文章:

python 数字列表错误地转换为字符串

python - 机器人框架: Create template

java - ClientBundle 不加载 ImageResource

python - 在 App Engine 上使用 Python2.7 时,可以引用每个请求全局变量吗?

html - 使用 shell 脚本发送 HTML 邮件

html - 电子邮件正文中的多个 html 文件

python - `id`是python中的关键字吗?

python - TypeError : Fetch argument array has invalid type numpy. ndarray,必须是字符串或Tensor。 (不能将 ndarray 转换为张量或操作。)

python - 如何在 '- url:' 行中创建多个 url,在 google-app-engine 上

Javamail 抛出一些错误