Google App Engine

Sending Mail

The mail API provides two ways to send an email message: the mail.send_mail() function and the EmailMessage class.

The mail.send_mail() function takes the fields of the email message as parameters, including the sender, the recipients, the subject and the body of the message.

from google.appengine.api import mail

mail.send_mail(sender=" Support <>",
              to="Albert Johnson <>",
              subject="Your account has been approved",
Dear Albert:

Your account has been approved.  You can now visit and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The Team

The EmailMessage class provides the same functionality using objects. The fields of the email message can be passed to the EmailMessage constructor, and updated using attributes of the instance. The send() method sends the email message represented by the instance's attributes. An application can re-use an EmailMessage instance by modifying attributes and calling the send() method again.

from google.appengine.api import mail

message = mail.EmailMessage(sender=" Support <>",
                            subject="Your account has been approved") = "Albert Johnson <>"
message.body = """
Dear Albert:

Your account has been approved.  You can now visit and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The Team


For more information about the possible fields of an email message, see Email Message Fields.

Sending is asynchronous: The mail.send_mail() function and the EmailMessage send() method transmit the message data to the mail service, then return. The mail service queues the message, then attempts to send it, possibly retrying if the destination mail server is unavailable. Errors and bounce messages are sent to the sender address for the email message.

The email address of the sender, the From address. The sender address must be one of the following types:

  • The address of a registered administrator for the application. You can add administrators to an application using the Administration Console.
  • The address of the user for the current request signed in with a Google Account. You can determine the current user's email address with the Users API. The user's account must be a Gmail account, or be on a domain managed by Google Apps.
  • Any valid email receiving address for the app (such as
  • Any valid email receiving address of a domain account, such as Domain accounts are accounts outside of the Google domain with email addresses that do not end in or

To send an email from an existing domain account, add the domain account as a Developer to your application using the Administration Console. The account is then sent an email with an invite to develop the application, which you must accept to add the account to the application. It is possible that when you send emails from a domain account using Google servers, automated spam classifiers might believe the email address is being spoofed. If you run into this issue, set the SPF records for the domain to indicate that Google is a trusted source for your email. For instructions on how to do this, see SPF records in the Google Apps help articles.

Note: Your domain (e.g. needs to be explictly registered with Google Apps and verified before you can create and use domain accounts (e.g. Domain accounts do not need to be explicitly verified, since you will have verified the domain during the registration process. For more information about registering a domain, see Register a new domain.

Here's an example of sending a message from the currently signed-in user:

import webapp2
from google.appengine.api import mail
from google.appengine.api import users

class InviteFriendHandler(webapp2.RequestHandler):
    def post(self):
        user = users.get_current_user()
        if user is None:
          login_url = users.create_login_url(self.request.path)
        to_addr = self.request.get("friend_email")
        if not mail.is_email_valid(to_addr):
            # Return an error message...

        message = mail.EmailMessage()
        message.sender = = to_addr
        message.body = """
I've invited you to!

To accept this invitation, click the following link,
or copy and paste the URL into your browser's address

        """ % generate_invite_link(to_addr)


The development web server can send email messages if configured to do so with command-line options. It can use an SMTP server or the Sendmail application, if available. When your application is running on App Engine, it uses the App Engine mail service to send email messages. See the Development Web Server for more information.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.