Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .coverage
Binary file not shown.
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