Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

Sending Mail

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

This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to themigration guide to learn about your migration options for legacy bundled services.

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

Before you begin

You must register your sender emails as authorized senders. For moreinformation, seewho can send email.

Sending mail withmail.send_mail()

To send mail using themail.send_mail() function, use the fields of theemail message as parameters, including the sender, the recipients, the subjectand the body of the message. For example:

mail.send_mail(sender=sender_address,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 visithttp://www.example.com/ and sign in using your Google Account toaccess new features.Please let us know if you have any questions.The example.com Team""")

Sending mail withEmailMessage

To send mail using objects with theEmailMessage class, pass the fields of the email message tothe EmailMessage constructor and use attributes of the instance to updatemessage.

TheEmailMessage.send() method sends the email messagerepresented by the instance's attributes. An application can re-use anEmailMessage instance by modifying attributes and calling thesend() methodagain.

message=mail.EmailMessage(sender=sender_address,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 visithttp://www.example.com/ and sign in using your Google Account toaccess new features.Please let us know if you have any questions.The example.com Team"""message.send()

The following example demonstrates sending a message to confirm an emailaddress:

classUserSignupHandler(webapp2.RequestHandler):"""Serves the email address sign up form."""defpost(self):user_address=self.request.get('email_address')ifnotmail.is_email_valid(user_address):self.get()# Show the form again.else:confirmation_url=create_new_user_confirmation(user_address)sender_address=('Example.com Support<{}@appspot.gserviceaccount.com>'.format(app_identity.get_application_id()))subject='Confirm your registration'body="""Thank you for creating an account!Please confirm your email address by clicking on the link below:{}""".format(confirmation_url)mail.send_mail(sender_address,user_address,subject,body)

Sending bulk mail

See theBulk mail guidelines for considerations around sending bulkemail.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.