Sending mail with the Mail API

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

Before you begin

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

Sending mail

To send mail from your application:

  1. Use the mail.Message type to set the sender, recipient, subject, and body of the message.

  2. Send the email with the mail.Send function.

The following example sends an email message to the user as a confirmation that they have created a new account with the application:


import (
        "bytes"
        "fmt"
        "net/http"

        "google.golang.org/appengine/v2"
        "google.golang.org/appengine/v2/log"
        "google.golang.org/appengine/v2/mail"
)

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

const confirmMessage = `
Thank you for creating an account!
Please confirm your email address by clicking on the link below:

%s
`