我在閱讀有關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
但是不打印任何東西。
如果有人澄清如何接收電子郵件的作品,我將不勝感激。
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組,但沒有答案。)
這是logging.info(「從%s收到的電子郵件%」message.sender)的代碼行,記錄的東西? – systempuntoout 2010-11-20 15:24:38
是的;我不知道logging.info記錄信息到日誌控制檯:)所以,代碼似乎工作;現在我需要知道如何將電子郵件的內容寫入數據存儲區。謝謝! – Zeynel 2010-11-20 16:46:34