5

I am reading the tutorial about Receiving Mail. I updated the app.yaml file as instructed:

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

And created a 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()

I also have hw.py that I used to practice sending email. That one works.

Now I go to http://localhost:8081/_ah/admin/inboundmail and send an email to help@hello-1-world.appspotmail.com

Can anyone explain to me how I process this email? How do I access the content of the email? I have the code

self.response.out.write(txtmsg)

in handle_incoming_email.py but that does not print anything.

I would greatly appreciate if someone clarify how receiving email works.

For instance, 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)

as far as I understand sender is the email of the sender. So, in my case, how do I access the sender email address.

Also, why do I need to have a separate script to handle incoming mail? Why can't I put the ReceiveEmail handler in my hw.py script? If I do that, where do I put the line

application = webapp.WSGIApplication([
  ReceiveEmail.mapping()
], debug=True)

I would be grateful if you can help me with these questions.

(I asked the same question in GAE group but there were no answers.)

Community
  • 1
  • 1
Zeynel
  • 13,145
  • 31
  • 100
  • 145
  • Is this logging.info("Received email from %s" % message.sender) line of code, logging something? – systempuntoout Nov 20 '10 at 15:24
  • 1
    Yes; I did not know that logging.info was logging the info into the Log Console :) So, the code seems to be working; now I need to figure how to write the content of the email to datastore. Thanks! – Zeynel Nov 20 '10 at 16:46

1 Answers1

1

Is help@hello-1-world.appspotmail.com a valid google user? GAE can receive/send mails only from the google user of your application. Your code seems correct.

"Also, why do I need to have a separate script to handle incoming mail? Why can't I put the ReceiveEmail handler in my hw.py" -> the main script is to handle url request, I think is much clearer in this way.

Uberto
  • 2,712
  • 3
  • 25
  • 27
  • Thanks for the answer. I am confused about why `help@hello-1-world.appspotmail.com` needs to be a "valid google user." The tutorial says: "Your app can receive email at addresses of the following form: `string@appid.appspotmail.com` So it seems to me that `help@hello-1-world.appspotmail.com` conforms to that form. But what I don't understand is how to process this email. For instance, how do I print the body of the received email? – Zeynel Nov 20 '10 at 14:56
  • 1
    Your code is correct, if you run the app and email receiving is active you should see the body in the logs. Frankly I assumed that only google users where valid email, but I never checked. For sure only google users can be mail sender. Check also that mail is really active from the console. – Uberto Nov 20 '10 at 15:26
  • Sorry, I did not know what `logging.info()` did. Yes, I see in Log Console that the email I sent from `http://localhost:8081/_ah/admin/inboundmail` is received and logged in. Thanks for clarifying this. Now I will try to write the content to the datastore. – Zeynel Nov 20 '10 at 16:22
  • Your mail address is valid but you cannot send a mail to appspotmail.com and expect your development server to pick it up. So, both your code and the mail address you use seem totally fine. You can use the development server console to simulate incoming email messages. Go to http://localhost:8081/_ah/admin/ (or whatever port you are running it on). Click Inbound Mail on the left side, and you should be able to figure out the rest from there. Other than that, everything you need for parsing the emails can be found here: http://code.google.com/appengine/docs/python/mail/receivingmail.html – Jon Nylander Nov 20 '10 at 21:50