title: Receiving Mail in Google App Engine
body: <p>I am reading the tutorial about <a href="http://code.google.com/appengine/docs/python/mail/receivingmail.html" rel="nofollow noreferrer">Receiving Mail</a>. I updated the app.yaml file as instructed:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And created a <code>handle_incoming_email.py</code></p>
  
  <pre><code>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()
  </code></pre>
  
  <p>I also have <code>hw.py</code> that I used to practice sending email. That one works.</p>
  
  <p>Now I go to <code>http://localhost:8081/_ah/admin/inboundmail</code> and send an email to <code>help@hello-1-world.appspotmail.com</code></p>
  
  <p>Can anyone explain to me how I process this email? How do I access the content of the email? I have the code</p>
  
  <pre><code>self.response.out.write(txtmsg)
  </code></pre>
  
  <p>in <code>handle_incoming_email.py</code> but that does not print anything.</p>
  
  <p>I would greatly appreciate if someone clarify how receiving email works.</p>
  
  <p>For instance, <a href="https://stackoverflow.com/questions/3763630/match-email-sender-field-with-a-user-id-google-app-engine">in this question</a> </p>
  
  <pre><code>class MailHandler (InboundMailHandler):
    def receive(self, message):
      sender = message.sender
      user_account = db.GqlQuery("SELECT * FROM Task WHERE user = :1", sender).fetch(5)
  </code></pre>
  
  <p>as far as I understand <code>sender</code> is the email of the sender. So, in my case, how do I access the sender email address.</p>
  
  <p>Also, why do I need to have a separate script to handle incoming mail? Why can't I put the  <code>ReceiveEmail</code> handler in my <code>hw.py</code> script? If I do that, where do I put the line </p>
  
  <pre><code>application = webapp.WSGIApplication([
    ReceiveEmail.mapping()
  ], debug=True)
  </code></pre>
  
  <p>I would be grateful if you can help me with these questions.</p>
  
  <p>(<a href="http://groups.google.com/group/google-appengine/browse_thread/thread/e170a677bbfdb7ce?hl=en#" rel="nofollow noreferrer">I asked the same question</a> in GAE group but there were no answers.)</p>