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

时间:2017-09-01 14:48:43

标签: 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)并且有效......

1 个答案:

答案 0 :(得分:1)

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

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

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)