设置邮件以在Google App Engine上接收电子邮件 [英] Set up mail to receive emails on Google App Engine

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

问题描述

文件相当不完整。我无法设置入站电子邮件。以下是一些细节:



app.yaml:

 处理程序: 

- url:/_ah/mail/owner@oladic\.appspotmail\.com
脚本:handle_owner.py
登录:admin

- url:/_ah/mail/support@oladic\.appspotmail\.com
脚本:handle_support.py
登录:admin

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

- url:。*
脚本:main.py

inbound_services:
- mail

handle_catchall.py:

 #要更改此模板,请选择工具模板
#并在编辑器中打开模板。

导入日志记录,电子邮件

from google.appengine.api import mail
from google.appengine.ext import webapp
from google.appengine.ext .webapp.util import run_wsgi_app
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class LogSenderHandler(InboundMailHandler):
def receive(self,mail_message):
logging.info(============================)
logging.info(收到一条消息:+ mail_message.sender
plaintext_bodies = message.bodies('text / plain')
html_bodies = message.bodies('text / html')

for content_type,body in html_bodies:
decoded_html = body.decode()
logging.info(content_type:+ content_type)
logging.info(decoded_html:+ decoded_html)

附件= []
如果message.attachments:
如果isinstance(message.attachments [0],basestring):
attachments = [message.attachments]
else:
附件= message.attachments

logging.info(附件数量+ str(len(附件) )

附件中的内容
logging.info(plaintext_bodies:+ plaintext_bodies)
logging.info(filename:+ filename)
content

logging.info(--------------------------------)



def main():
application = webapp.WSGIApplication([LogSenderHandler.mapping()],debug = True)
wsgiref.handlers。 CGIHandler()。run(application)


如果__name__ =='__main__':
main()


解决方案

我检查过,发现了一些微不足道的错误/错误。将会更新。



没有更多的问题。以下是工作示例的代码:



handle_catchall.py:



 导入日志记录,电子邮件
import wsgiref.handlers
import exception

from google.appengine.api import mail
from google.appengine.ext import webapp
从google.appengine.ext.webapp.util导入run_wsgi_app
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class LogSenderHandler(InboundMailHandler):
def receive(self ,mail_message):
logging.info(============================)
logging.info(Received a mail_message from:+ mail_message.sender)
logging.info(电子邮件主题:+ mail_message.subject)
logging.info(电子邮件已发送到:+ str.join(str(mail_message.to),','))

try:
logging.info(该电子邮件被CC编辑为:+ str。 join(str(mail_message.cc),','))
except exceptions.AttributeError:
logging.info(电子邮件没有CC-ed收件人)

try:
logging.info(电子邮件发送:+ str(mail_message。日期))
except exceptions.AttributeError:
logging.info(电子邮件没有指定发送日期!!!)

plaintext_bodies = mail_message.bodies('text /
html_bodies = mail_message.bodies('text / html')

for content_type,body in html_bodies:
decoded_html = body.decode()
logging .info(content_type:+ content_type)
logging.info(decoded_html:+ decoded_html)
plaintext_bodies

附件= []
#hasattr ,'property')
#http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python
try:
如果mail_message.attachments:
if isinstance(mail_message.a ttachments [0],basestring):
attachments = [mail_message.attachments]
else:
attachments = mail_message.attachments
except exceptions.AttributeError:
logging.info (此电子邮件没有附件)

logging.info(附件数:+ str(len(附件)))

的文件名,附件:
#logging.info(plaintext_bodies:+ plaintext_bodies)
logging.info(filename:+ filename)
content

logging.info --------------------------------)



def main():
application = webapp.WSGIApplication([LogSenderHandler.mapping()],debug = True)
wsgiref.handlers.CGIHandler()。run(application)


如果__name__ =='__main__':
main()


The documentation is rather incomplete. I could not set up the inbound email. Here are some details:

app.yaml:

handlers:

- url: /_ah/mail/owner@oladic\.appspotmail\.com
  script: handle_owner.py
  login: admin

- url: /_ah/mail/support@oladic\.appspotmail\.com
  script: handle_support.py
  login: admin

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

- url: .*
  script: main.py

inbound_services:
- mail

handle_catchall.py:

# To change this template, choose Tools | Templates
# and open the template in the editor.

import logging, email

from google.appengine.api import mail
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        logging.info("================================")
        logging.info("Received a message from: " + mail_message.sender)
        plaintext_bodies = message.bodies('text/plain')
        html_bodies = message.bodies('text/html')

        for content_type, body in html_bodies:
            decoded_html = body.decode()
            logging.info("content_type: " + content_type)
            logging.info("decoded_html: " + decoded_html)

        attachments = []
        if message.attachments:
            if isinstance(message.attachments[0], basestring):
                attachments = [message.attachments]
            else:
                attachments = message.attachments

        logging.info("number of attachments: " + str(len(attachments)))

        for filename, content in attachments:
            logging.info("plaintext_bodies: " + plaintext_bodies)
            logging.info("filename: " + filename)
            content

        logging.info("--------------------------------")



def main():
    application = webapp.WSGIApplication([LogSenderHandler.mapping()], debug=True)
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()

解决方案

I checked that found some trivial bugs/mistakes. Will update later.

No more issues. Here's the code of working example:

handle_catchall.py:

import logging, email
import wsgiref.handlers
import exceptions

from google.appengine.api import mail
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        logging.info("================================")
        logging.info("Received a mail_message from: " + mail_message.sender)
        logging.info("The email subject: " + mail_message.subject)
        logging.info("The email was addressed to: " + str.join(str(mail_message.to), ', '))

        try:
            logging.info("The email was CC-ed to: " + str.join(str(mail_message.cc), ', '))
        except exceptions.AttributeError :
            logging.info("The email has no CC-ed recipients")

        try:
            logging.info("The email was send on: " + str(mail_message.date))
        except exceptions.AttributeError :
            logging.info("The email has no send date specified!!!")

        plaintext_bodies = mail_message.bodies('text/plain')
        html_bodies = mail_message.bodies('text/html')

        for content_type, body in html_bodies:
            decoded_html = body.decode()
            logging.info("content_type: " + content_type)
            logging.info("decoded_html: " + decoded_html)
            plaintext_bodies

        attachments = []
        # hasattr(a, 'property')
        # http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python
        try:
            if mail_message.attachments :
                if isinstance(mail_message.attachments[0], basestring):
                    attachments = [mail_message.attachments]
                else:
                    attachments = mail_message.attachments
        except exceptions.AttributeError :
            logging.info("This email has no attachments.")

        logging.info("number of attachments: " + str(len(attachments)))

        for filename, content in attachments:
            #logging.info("plaintext_bodies: " + plaintext_bodies)
            logging.info("filename: " + filename)
            content

        logging.info("--------------------------------")



def main():
    application = webapp.WSGIApplication([LogSenderHandler.mapping()], debug=True)
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆