views:

1112

answers:

4

We have our mail setup with google apps. We want to be able to run some regular expressions on incoming mail and process this information.

Is this possible today with Google App Engine? Does google provide some kind of infrastructure that can do this?

+1  A: 

Update: It's now supported.

Processing incoming email is not yet supported. It is however on their roadmap: http://code.google.com/appengine/docs/roadmap.html

Sam
Is there any other ways you would recommend how I go about achieving this task
No really, i guess you could screen scrape the mail from a html mail client and then use something like this: http://schedulerservice.appspot.com/about Best to either use another platform or wait for the roadmap to be implemented.
Sam
A: 

You could setup an email account and have an external server (one you create and host outside of AE) access the gmail account via IMAP. Your "mail server" then reads the messages and accesses the /email API of your app on AE.

Python has an email module, so you could post the entire message there, or if that doesn't work (due to whatever restrictions), you could preprocess it on your mail server and post the simplified version to your app.

The downside is that you'll have to resort to polling for information, but that should be ok since email is accepted to have somewhat of a delay.

Richard Levasseur
Your not allowed to use the socket lib, would that stop imap from working?
Sam
I mean to say, the external server (one you host yourself) isn't running on app engine, so it can access the socket lib. I edited the answer to be a bit clearer
Richard Levasseur
+2  A: 

Google don't currently support handling email in App Engine, though it is on the roadmap. In the meantime, services like smtp2web will handle it for you (disclaimer: I wrote smtp2web).

Nick Johnson
Thats a pretty cool service.
Richard Levasseur
nick, does smtp2web work still, [I keep getting errors](http://stackoverflow.com/questions/3553246/are-there-any-smtp-to-http-free-services-email-to-post). Any help would be amazing :)
viatropos
+4  A: 

from google documentation here:

Receiving Mail

Your app can receive email at addresses of the following form:

[email protected]

Note that even if your app is deployed on a custom domain, your app can't receive email sent to addresses on that domain. Email messages are sent to your app as HTTP requests. These requests are generated by App Engine and posted to your app. In your app's configuration, you specify handlers that will be called to handle these HTTP requests. In your handlers, you receive the MIME data for email messages, which you then parse into its individual fields.

Email messages are sent to your app as HTTP POST requests using the following URL:

/_ah/mail/address

where address is a full email address, including domain name. The ability to receive mail in your app is disabled by default. To enable your app to receive mail, you must specify that you want this service enabled in your app.yaml file by including this:

inbound_services:
- mail

The Python SDK defines InboundMailHandler, a webapp class for handling incoming email. To use InboundMailHandler, you subclass it and override the receive() method. The receive() method is called with an argument of class InboundEmailMessage, another class defined by the Python SDK.

InboundMailHandler is in the google.appengine.ext.webapp.mail_handlers package. You can create an instance of InboundEmailMessage like this:

import logging, email
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        logging.info("Received a message from: " + mail_message.sender)

The InboundEmailMessage object includes attributes to access other message fields:

subject contains the message subject.
sender is the sender's email address.
to is a list of the message's primary recipients.
cc contains a list of the cc recipients.
date returns the message date.
attachments is a list of file attachments, possibly empty. Each value in the list is a tuple of two elements: the filename and the file contents.
original is the complete message, including data not exposed by the other fields such as email headers, as a Python email.message.Message.
Gabriel