2010-11-20 49 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

相關問題