Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src/appengine_python_standard.egg-info
.DS_Store
build/*
appengine_python_standard-*-py*.egg
.coverage
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="appengine-python-standard",
version="1.1.10",
version="2.0.0",
author="Google LLC",
description="Google App Engine services SDK for Python 3",
long_description=long_description,
Expand Down
77 changes: 68 additions & 9 deletions src/google/appengine/api/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
from email.mime.text import MIMEText
import functools
import logging
import os
import smtplib
import typing

from google.appengine.api import api_base_pb2
Expand Down Expand Up @@ -1197,20 +1199,77 @@ def ToMIMEMessage(self):
return self.to_mime_message()

def send(self, make_sync_call=apiproxy_stub_map.MakeSyncCall):
"""Sends an email message via the Mail API.
"""Sends an email message via the Mail API or SMTP.

If the 'USE_SMTP_MAIL_SERVICE' environment variable is set to 'true', this
method will send the email via SMTP using credentials from other
environment variables. Otherwise, it falls back to the App Engine Mail API.

Args:
make_sync_call: Method that will make a synchronous call to the API proxy.
"""
message = self.ToProto()
response = api_base_pb2.VoidProto()
if os.environ.get('APPENGINE_USE_SMTP_MAIL_SERVICE') == 'true':
logging.info('Sending email via SMTP.')
mime_message = self.to_mime_message()

# The Bcc header should not be in the final message.
if 'Bcc' in mime_message:
del mime_message['Bcc']

recipients = []
if isinstance(self, AdminEmailMessage):
admin_emails = os.environ.get('APPENGINE_ADMIN_EMAIL_RECIPIENTS')
if admin_emails:
recipients.extend(admin_emails.split(','))
else:
if hasattr(self, 'to'):
recipients.extend(_email_sequence(self.to))
if hasattr(self, 'cc'):
recipients.extend(_email_sequence(self.cc))
if hasattr(self, 'bcc'):
recipients.extend(_email_sequence(self.bcc))

if not recipients:
raise MissingRecipientsError()

try:
host = os.environ['APPENGINE_SMTP_HOST']
port = int(os.environ.get('APPENGINE_SMTP_PORT', 587))
user = os.environ.get('APPENGINE_SMTP_USER')
password = os.environ.get('APPENGINE_SMTP_PASSWORD')
use_tls = os.environ.get('APPENGINE_SMTP_USE_TLS', 'true').lower() == 'true'

with smtplib.SMTP(host, port) as server:
if use_tls:
server.starttls()
if user and password:
server.login(user, password)
server.send_message(mime_message, from_addr=self.sender, to_addrs=recipients)
logging.info('Email sent successfully via SMTP.')

except smtplib.SMTPAuthenticationError as e:
logging.error('SMTP authentication failed: %s', e.smtp_error)
raise InvalidSenderError(f'SMTP authentication failed: {e.smtp_error}')
except (smtplib.SMTPException, OSError) as e:
logging.error('Failed to send email via SMTP: %s', e)
raise Error(f'Failed to send email via SMTP: {e}')
except KeyError as e:
logging.error('Missing required SMTP environment variable: %s', e)
raise Error(f'Missing required SMTP environment variable: {e}')

try:
make_sync_call('mail', self._API_CALL, message, response)
except apiproxy_errors.ApplicationError as e:
if e.application_error in ERROR_MAP:
raise ERROR_MAP[e.application_error](e.error_detail)
raise e
else:
logging.info('Sending email via App Engine Mail API.')
message = self.ToProto()
response = api_base_pb2.VoidProto()

try:
make_sync_call('mail', self._API_CALL, message, response)
logging.info('Email sent successfully via App Engine Mail API.')
except apiproxy_errors.ApplicationError as e:
logging.error('App Engine Mail API error: %s', e)
if e.application_error in ERROR_MAP:
raise ERROR_MAP[e.application_error](e.error_detail)
raise e


def Send(self, *args, **kwds):
Expand Down
Loading
Loading