本文介绍了在Google App Engine中接收邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关的教程。我按照说明更新了app.yaml文件:

 应用程序:hello-1-world 
版本:1
运行时:python
api_version:1

处理程序:
- url:/favicon.ico
static_files:static / images / favicon.ico
上传:static / images / favicon.ico

- url:/_ah/mail/.+
脚本:handle_incoming_email.py
登录:admin

- url:/.*
script:hw.py

inbound_services:
- mail

并创建了一个 handle_incoming_email.py

  import cgi 
import os
import log
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine .ext.webapp.util import_wsgi_app
from google.appengine.ext import db
from google.appengine.api import mail
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class ReceiveEmail(InboundMailHandler):
def接收(自我,消息):
logging.info(从%s收到的电子邮件%message.sender)
plaintext = message.bodies(content_type ='text / plain')
纯文本:
txtmsg =
txtmsg = text [1] .decode()
logging.info(Body is%s%txtmsg)
self.response .out.write(txtmsg)

application = webapp.WSGIApplication([
ReceiveEmail.mapping()
],debug = True)

def main():
run_wsgi_app(application)
如果__name__ ==__main__:
main()

我也有用于练习发送电子邮件的 hw.py



现在我去 http:// localhost:8081 / _ah / admin / inboundmail 和发送电子邮件至 [email protected]



任何人都可以向我解释我处理此电子邮件?如何访问电子邮件的内容?我有代码

  self.response.out.write(txtmsg)
/ pre>

handle_incoming_email.py 中,但不打印任何内容。



如果有人澄清接收电子邮件的工作原理,我将非常感激。



例如,

类MailHandler(InboundMailHandler):
def receive(self,message):
sender = message.sender
user_account = db.GqlQuery(SELECT * FROM Task WHERE user =:1,发件人).fetch(5)

据我所知发件人是发件人的电子邮件。所以,在我的情况下,如何访问发件人的电子邮件地址。



另外,为什么我需要一个单独的脚本来处理收到的邮件?为什么我不能将 ReceiveEmail 处理程序放在我的 hw.py

  application = webapp.WSGIApplication([
ReceiveEmail.mapping )
],debug = True)

如果可以帮助我,我将不胜感激这些问题。



(,但没有答案。)

解决方案

p>是[email protected]一个有效的谷歌用户? GAE只能从您的应用程序的谷歌用户接收/发送邮件。
您的代码似乎正确。



另外,为什么我需要一个单独的脚本来处理收到的邮件?为什么我不能把ReceiveEmail处理程序在我的hw.py - >主要脚本是处理url请求,我认为这样更清楚。


I am reading the tutorial about Receiving Mail. I updated the app.yaml file as instructed:

application: hello-1-world
version: 1
runtime: python
api_version: 1

handlers:
- url: /favicon.ico
  static_files: static/images/favicon.ico
  upload: static/images/favicon.ico

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

- url: /.*
  script: hw.py

inbound_services:
- mail

And created a handle_incoming_email.py

import cgi
import os
import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import mail
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class ReceiveEmail(InboundMailHandler):
    def receive(self,message):
        logging.info("Received email from %s" % message.sender)
        plaintext = message.bodies(content_type='text/plain')
        for text in plaintext:
            txtmsg = ""
            txtmsg = text[1].decode()
            logging.info("Body is %s" % txtmsg)
            self.response.out.write(txtmsg)

application = webapp.WSGIApplication([
  ReceiveEmail.mapping()
], debug=True)

def main():
    run_wsgi_app(application)
if __name__ == "__main__":
    main()

I also have hw.py that I used to practice sending email. That one works.

Now I go to http://localhost:8081/_ah/admin/inboundmail and send an email to [email protected]

Can anyone explain to me how I process this email? How do I access the content of the email? I have the code

self.response.out.write(txtmsg)

in handle_incoming_email.py but that does not print anything.

I would greatly appreciate if someone clarify how receiving email works.

For instance, in this question

class MailHandler (InboundMailHandler):
  def receive(self, message):
    sender = message.sender
    user_account = db.GqlQuery("SELECT * FROM Task WHERE user = :1", sender).fetch(5)

as far as I understand sender is the email of the sender. So, in my case, how do I access the sender email address.

Also, why do I need to have a separate script to handle incoming mail? Why can't I put the ReceiveEmail handler in my hw.py script? If I do that, where do I put the line

application = webapp.WSGIApplication([
  ReceiveEmail.mapping()
], debug=True)

I would be grateful if you can help me with these questions.

(I asked the same question in GAE group but there were no answers.)

解决方案

Is [email protected] a valid google user? GAE can receive/send mails only from the google user of your application.Your code seems correct.

"Also, why do I need to have a separate script to handle incoming mail? Why can't I put the ReceiveEmail handler in my hw.py" -> the main script is to handle url request, I think is much clearer in this way.

这篇关于在Google App Engine中接收邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:54