Email messages sent to your app are implemented as HTTP requests containing MIME data. To process incoming email messages, you associate email addresses with script handlers in your app configuration, then include the handlers in your app's code. Incoming email generates HTTP requests, which are passed to the appropriate scripts.
For more information on the Mail service, see the Mail API Overview
Configuring your application to receive email
When you create a new app, incoming email is disabled by default. To enable the incoming email
service, you must modify your app.yaml file.
- Add an
inbound_servicessection that enables the incoming email service. For example:If you don't enable incoming email by including this section in your configuration file, then incoming email is disabled, and email messages sent to the app are ignored.
- Add mappings that associate URL-mapped email addresses with script handlers.
Your app can receive email at addresses of the following form:
string@appid.appspotmail.com
Email messages are sent to your app as HTTP POST requests using the following URL, where [ADDRESS] is a full email address, including domain name:
/_ah/mail/[ADDRESS]
To handle incoming email in your app, map email URLs to handlers in the
app.yamlfile:In the above example,
/_ah/mail/.+matches all email addressed to the app. If you prefer, you can set up multiple handlers for different email addresses, as in the following example:URLs of incoming email messages are matched to this list from first to last, so if an email message URL matches more than one pattern, the first matching handler will be the one executed. This allows you to include a "catchall" handler as the last mapping. The handlers run in the default module (or application version).
Handling incoming email
The Python SDK defines InboundMailHandler, a webapp class for handling incoming
email. InboundMailHandler is in the
google.appengine.ext.webapp.mail_handlers package.
To use InboundMailHandler:
- Create a subclass for
InboundMailHandlerand override thereceive()method. - Call the
receive()method with an argument ofclassInboundEmailMessage, defined by the Python SDK.
For example, you can create an instance of InboundEmailMessage like this:
Note: Even if you are using the webapp2 framework, you still need to use the
InboundMailHandler class provided by the old webapp framework. This handler is
specific to the App Engine mail service, whereas webapp2 is provided by a third party.
InboundMailHandler contains a mapping() convenience class method that returns a
pair matching all incoming email addresses to the mail handler (and of course you can call it on
any subclass of InboundMailHandler you code):
The InboundEmailMessage object (mail_message in this example) contains
the email message. Its bodies() method returns the bodies within the message. If you
call bodies() without arguments, it returns an iterator that yields HTML bodies first,
then plain text bodies. If you want just HTML or just plain text, you can pass an argument to
bodies():
The InboundEmailMessage object includes attributes to access other message fields:
subjectcontains the message subject.senderis the sender's address e.g."Nobody <[email protected]>".tois a comma-separated list of the message's primary recipients e.g."Joe <[email protected]>, Bill <[email protected]>".cccontains a comma-separated list of the cc recipients e.g."Joe <[email protected]>, Bill <[email protected]>".datereturns the message date.attachmentsis a list ofAttachmentobjects, possibly empty.originalis the complete message, including data not exposed by the other fields such as email headers, as a Python email.message.Message.
Simulating incoming messages with the local development server
Once you set up your app to handle incoming email, you can use the development server console to simulate incoming email messages:
- Access the development server as an administrator by going to http://localhost:8080/_ah/login and selecting Sign in as administrator .
- In the development server, click Inbound Mail on the left side.
- Fill out the form that appears, and click Send Email.
To learn more, including how to get the development server running, see The Python Development Server.