2010-11-20 141 views
5

我在阅读有关Receiving Mail的教程。我的app.yaml文件更新的指示:在Google App Engine中接收邮件

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 

而且创造了一个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() 

我也有hw.py,我用来练习发送电子邮件。那个可以工作。

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

谁能向我解释我是如何处理该邮件?我如何访问电子邮件的内容?我的代码是

self.response.out.write(txtmsg) 

in handle_incoming_email.py但是不打印任何东西。

如果有人澄清如何接收电子邮件的作品,我将不胜感激。

例如,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) 

据我了解sender是发件人的电子邮件。所以,就我而言,我如何访问发件人的电子邮件地址。

此外,为什么我需要一个单独的脚本来处理传入的邮件?为什么我不能将ReceiveEmail处理程序放在我的hw.py脚本中?如果我这样做,我在哪里放线

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

如果你能帮助我解决这些问题,我将不胜感激。

I asked the same question在GAE组,但没有答案。)

+0

这是logging.info(“从%s收到的电子邮件%”message.sender)的代码行,记录的东西? – systempuntoout 2010-11-20 15:24:38

+1

是的;我不知道logging.info记录信息到日志控制台:)所以,代码似乎工作;现在我需要知道如何将电子邮件的内容写入数据存储区。谢谢! – Zeynel 2010-11-20 16:46:34

回答

1

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

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

+0

感谢您的回答。我很困惑为什么'help @ hello-1-world.appspotmail.com'需要成为“有效的谷歌用户”。该教程说:“您的应用程序可以通过以下格式的地址接收电子邮件:'string @ appid.appspotmail.com'因此,在我看来,”help @ hello-1-world.appspotmail.com“符合该表单。但我不明白的是如何处理这封电子邮件,例如,如何打印收到的电子邮件的正文? – Zeynel 2010-11-20 14:56:46

+1

您的代码是正确的,如果您运行应用程序并且电子邮件接收处于活动状态,您应该看到正文我只是假定只有谷歌用户有效的电子邮件,但我从来没有检查过,当然只有谷歌用户可以是邮件发件人,请检查该邮件是否真的是主动从控制台 – Uberto 2010-11-20 15:26:02

+0

对不起,我不知道什么'日志记录.info()'做过。是的,我在Log Console中看到从“http:// localhost:8081/_ah/admin/inboundmail”发送的电子邮件已收到并登录。感谢您澄清此问题。现在我将尝试将内容写入数据存储区。 – Zeynel 2010-11-20 16:22:14