English | Site Directory

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@example.com",
              to="Albert Johnson <Albert.Johnson@example.com>",
              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="support@example.com",
                            subject="Your account has been approved")

message.to = "Albert Johnson <Albert.Johnson@example.com>"
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 sender address can be either the email address of a registered administrator for the application, or the email address of the current signed-in user (the user making the request that is sending the message). Here's an example of sending a message from the current signed-in user, using the login_required annotation to redirect the user to the sign-in page if they are not signed in:

from google.appengine.api import mail
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import login_required

class InviteFriendHandler(webapp.RequestHandler):
  @login_required
  def post(self):
    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 = users.get_current_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.