Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



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="Example.com Support <[email protected]>",
              to="Albert Johnson <[email protected]>",
              subject="Your account has been approved",
              body="""
Dear Albert:

Your example.com account has been approved.  You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The example.com 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="Example.com Support <[email protected]>",
                            subject="Your account has been approved")

message.to = "Albert Johnson <[email protected]>"
message.body = """
Dear Albert:

Your example.com account has been approved.  You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The example.com Team
"""

message.send()

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 [email protected]).
  • Any valid email receiving address of a domain account, such as [email protected] . Domain accounts are accounts outside of the Google domain with email addresses that do not end in @gmail.com or @APP-ID.appspotmail.com.

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. example.com) needs to be explictly registered with Google Apps and verified before you can create and use domain accounts (e.g. [email protected]). 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)
          self.redirect(login_url)
          return
        to_addr = self.request.get("friend_email")
        if not mail.is_email_valid(to_addr):
            # Return an error message...
            pass

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

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

%s
        """ % generate_invite_link(to_addr)

        message.send()

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.