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 <firstname.lastname@example.org>", 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="Example.com Support <email@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()
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.
See the Overview section for information on which email addresses you can use for the Sender address.
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.