【问题标题】:Set up mail to receive emails on Google App Engine设置邮件以在 Google App Engine 上接收电子邮件
【发布时间】:2010-01-27 18:21:53
【问题描述】:

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

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()

【问题讨论】:

    标签: google-app-engine email


    【解决方案1】:

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

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

    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()
    

    【讨论】:

    • 在我的代码中 :) 我不得不处理一些问题,比如没有附件和 cc 属性时。
    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2014-11-05
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多