如何使用googleappengine的Python邮件服务找到请求的URL?

2024-03-28 11:00:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我的googleappengine应用程序希望存储各种收到的电子邮件,包括电子邮件的收件人。我是想知道如何找到收件人的网址,所以我想知道。在

在应用程序yaml有:

inbound_services:
- mail
handlers:
- url: /_ah/mail/.+ 
  script: handle_incoming_email.py 
  login: admin

Python脚本具有:

^{pr2}$

因此,如果电子邮件发送到john@myapp.appspot.com,收件人将是john@myapp.appspot.com。如果电子邮件发送到jane@myapp.appspot.com,收件人将是jane@myapp.appspot.com等等

我知道我可以筛选邮件_消息。收件人字段,但我宁愿查看实际传入的URL。看起来应该很简单,但我想不通。在


Tags: com应用程序yaml电子邮件handlersservicemailjohn
2条回答

好的,转到源代码来了解receive()和mapping()的作用。最后我做了我想做的事情:

class MyEmailHandler(InboundMailHandler):
    def post(self,recipient):
        # 'recipient' is the captured group from the below webapp2 route
        mail_message = mail.InboundEmailMessage(self.request.body)
        # do stuff with mail_message based on who recipient is

app = webapp2.WSGIApplication(
    [(r'/_ah/mail/(.+)@.+\.appspotmail\.com',MyEmailHandler)],
    debug=True)

webapp2路由器允许捕获组,并将其作为参数发送给处理程序。这里捕获的组是中的“收件人”recipient@myapp.appspotmail.com。但是您不能使用mapping()便利函数(在本例中它没有什么作用)或处理程序中的receive方法(它实际上只是从请求中获取电子邮件并将其放入要接收的参数中)

地址在处理程序的URL中,您可以查看self.request.path要检索它,但确实应该使用mail_message获取此值。在

相关问题 更多 >