Parsing incoming mail with Google App Engine?

We have mail settings with Google Apps. We want to be able to run regular expressions for incoming mail and process this information.

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

+4
source share
6 answers

from the google documentation here :

Receiving mail

Your application can receive emails at the addresses of the following form:

string@appid.appspotmail.com 

Please note that even if your application is deployed in a custom domain, your application cannot receive email sent to addresses in this domain. Email messages are sent to your application as HTTP requests. These requests are generated by App Engine and sent to your application. In the application configuration, you specify the handlers that will be called to process these HTTP requests. In your handlers, you get MIME data for email messages, which are then processed in separate fields.

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

 /_ah/mail/address 

where address is the full email address, including the domain name. The ability to receive mail in your application is disabled by default. In order for your application to receive mail, you must indicate that you want to enable this service in the app.yaml file by specifying this:

 inbound_services: - mail 

The Python SDK defines InboundMailHandler, the 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 the InboundEmailMessage class, another class defined by the Python SDK.

InboundMailHandler is located in the google.appengine.ext.webapp.mail_handlers package. You can create an instance of InboundEmailMessage as follows:

 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 for accessing other message fields:

 subject contains the message subject. sender is the sender email address. to is a list of the message 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. 
+11
source

Update: now supported .

Incoming mail processing is not yet supported. However, in their roadmap: http://code.google.com/appengine/docs/roadmap.html

+3
source

Google currently does not support email processing in App Engine, although it is located on the roadmap. In the meantime, services like smtp2web will handle it for you (disclaimer: I wrote smtp2web).

+2
source

You can set up an email account and have an external server (the one you create and host outside of AE) to access your gmail account through IMAP. Your “mail server” then reads the messages and accesses your application’s API / email in AE.

Python has an email module, so you can post the entire message there or if it doesn't work (due to any restrictions), you can pre-process it on your mail server and publish a simplified version for your application.

/ p>

The disadvantage is that you need to resort to a survey to get information, but this should be good, since email is considered somewhat delayed.

0
source

As another answer option from Gabriel, I would recommend using the go App Engine environment to send and receive mail with the mail API .

From the doc:

Receiving mail

Your application can receive emails at the addresses of the following form:

 anything@appid.appspotmail.com 

Compare with the configuration of incoming mail processing in python , as discussed here , including incoming mail in your app.yaml application app.yaml quite simple:

 inbound_services: - mail 

Name your application file as mail.go , then register the handler on the path /_ah/mail/ and read the email data from *http.Requestlike using net/mail :

 func incomingMail(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) defer r.Body.Close() var b bytes.Buffer if _, err := b.ReadFrom(r.Body); err != nil { log.Errorf(ctx, "Error reading body: %v", err) return } log.Infof(ctx, "Received mail: %v", b) } 

Sending mail

Follow this guideline to register sender emails as authorized senders

Use the mail.Message type to set the sender, recipient, subject and body of the message.
Send an email using the mail.Send function.

 func confirm(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) addr := r.FormValue("email") url := createConfirmationURL(r) msg := &mail.Message{ Sender: "Example.com Support < support@example.com >", To: []string{addr}, Subject: "Confirm your registration", Body: fmt.Sprintf(confirmMessage, url), } if err := mail.Send(ctx, msg); err != nil { log.Errorf(ctx, "Couldn't send email: %v", err) } } 

Expand

A complete sample code for receiving and sending is available here on GitHub:

GoogleCloudPlatform / golang-samples / docs / appengine / mail / mail.go

To clone the sample code, go to Console . Click the button to open Cloud Shell : enter image description here then similarly this quick start enter the following steps:

 $ SOURCEDIR=https://github.com/GoogleCloudPlatform/golang-samples.git $ TUTORIALDIR=~/src/your-application-id/go_gae_samples $ git clone $SOURCEDIR $TUTORIALDIR $ cd $TUTORIALDIR $ git checkout master $ cat docs/appengine/mail/app.yaml $ cat docs/appengine/mail/mail.go $ goapp serve docs/appengine/mail/app.yaml 

Here you can access the application on port 8080 using Web preview Web preview .
To complete, press Ctrl+C in Cloud Shell .
Finally, you can deploy your application

 goapp deploy -application your-application-id -version 0 

Click url to view it

 http://your-application-id.appspot.com/ 

Then send an email to anything@your-application-id.appspotmail.com if it works.

0
source

All Articles