Receiving mail with the Mail API

This guide describes how to use the Mail API to receive mail.

Before you begin

You must register your sender emails as authorized senders. For more information, see who can send email.

Receiving mail

You can set up your app to receive incoming email at addresses in the following format:

anything@appid.appspotmail.com

To receive email:

  1. Enable incoming mail in your app's app.yaml file:

    inbound_services:
    - mail
  2. Set up a handler to process incoming emails, which are supplied to your app as MIME data in an HTTP POST request.

    1. In your app, register a handler to the /_ah/mail/ path:

      func init() {
      	http.HandleFunc("/_ah/mail/", incomingMail)
      }
      
    2. In the handler, read the email's data from the *http.Request:

      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)
      }
      

    You can use the net/mail package in the standard library to parse mail messages.