Skip to content

Commit 5c8c0b7

Browse files
abhishekr700YashBanka
authored andcommitted
Refactor: Prefix SMTP environment variables with APPENGINE_
This change prefixes all environment variables related to the external SMTP mail service with `APPENGINE_` to improve namespacing and prevent potential conflicts in the deployment environment. The following variables have been renamed: - USE_SMTP_MAIL_SERVICE -> APPENGINE_USE_SMTP_MAIL_SERVICE - SMTP_HOST -> APPENGINE_SMTP_HOST - SMTP_PORT -> APPENGINE_SMTP_PORT - SMTP_USER -> APPENGINE_SMTP_USER - SMTP_PASSWORD -> APPENGINE_SMTP_PASSWORD - SMTP_USE_TLS -> APPENGINE_SMTP_USE_TLS - ADMIN_EMAIL_RECIPIENTS -> APPENGINE_ADMIN_EMAIL_RECIPIENTS The changes have been applied to both the SDK implementation in `mail.py` and the corresponding unit tests in `mail_unittest.py
1 parent 959715b commit 5c8c0b7

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

src/google/appengine/api/mail.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ def send(self, make_sync_call=apiproxy_stub_map.MakeSyncCall):
12081208
Args:
12091209
make_sync_call: Method that will make a synchronous call to the API proxy.
12101210
"""
1211-
if os.environ.get('USE_SMTP_MAIL_SERVICE') == 'true':
1211+
if os.environ.get('APPENGINE_USE_SMTP_MAIL_SERVICE') == 'true':
12121212
logging.info('Sending email via SMTP.')
12131213
mime_message = self.to_mime_message()
12141214

@@ -1218,7 +1218,7 @@ def send(self, make_sync_call=apiproxy_stub_map.MakeSyncCall):
12181218

12191219
recipients = []
12201220
if isinstance(self, AdminEmailMessage):
1221-
admin_emails = os.environ.get('ADMIN_EMAIL_RECIPIENTS')
1221+
admin_emails = os.environ.get('APPENGINE_ADMIN_EMAIL_RECIPIENTS')
12221222
if admin_emails:
12231223
recipients.extend(admin_emails.split(','))
12241224
else:
@@ -1233,11 +1233,11 @@ def send(self, make_sync_call=apiproxy_stub_map.MakeSyncCall):
12331233
raise MissingRecipientsError()
12341234

12351235
try:
1236-
host = os.environ['SMTP_HOST']
1237-
port = int(os.environ.get('SMTP_PORT', 587))
1238-
user = os.environ.get('SMTP_USER')
1239-
password = os.environ.get('SMTP_PASSWORD')
1240-
use_tls = os.environ.get('SMTP_USE_TLS', 'true').lower() == 'true'
1236+
host = os.environ['APPENGINE_SMTP_HOST']
1237+
port = int(os.environ.get('APPENGINE_SMTP_PORT', 587))
1238+
user = os.environ.get('APPENGINE_SMTP_USER')
1239+
password = os.environ.get('APPENGINE_SMTP_PASSWORD')
1240+
use_tls = os.environ.get('APPENGINE_SMTP_USE_TLS', 'true').lower() == 'true'
12411241

12421242
with smtplib.SMTP(host, port) as server:
12431243
if use_tls:

tests/google/appengine/api/mail_unittest.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,12 +1636,12 @@ def FakeMakeSyncCall(service, method, request, response):
16361636
def testSendEmailViaSmtp(self, mock_smtp):
16371637
"""Tests that mail.send_mail uses SMTP when configured."""
16381638
environ = {
1639-
'USE_SMTP_MAIL_SERVICE': 'true',
1640-
'SMTP_HOST': 'smtp.example.com',
1641-
'SMTP_PORT': '587',
1642-
'SMTP_USER': 'user',
1643-
'SMTP_PASSWORD': 'password',
1644-
'SMTP_USE_TLS': 'true',
1639+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
1640+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
1641+
'APPENGINE_SMTP_PORT': '587',
1642+
'APPENGINE_SMTP_USER': 'user',
1643+
'APPENGINE_SMTP_PASSWORD': 'password',
1644+
'APPENGINE_SMTP_USE_TLS': 'true',
16451645
}
16461646

16471647
with mock.patch.dict('os.environ', environ):
@@ -1681,12 +1681,12 @@ def testSendEmailViaSmtp(self, mock_smtp):
16811681
def testSendEmailViaSmtp_MultipleRecipients(self, mock_smtp):
16821682
"""Tests that mail.send_mail handles multiple recipients via SMTP."""
16831683
environ = {
1684-
'USE_SMTP_MAIL_SERVICE': 'true',
1685-
'SMTP_HOST': 'smtp.example.com',
1686-
'SMTP_PORT': '587',
1687-
'SMTP_USER': 'user',
1688-
'SMTP_PASSWORD': 'password',
1689-
'SMTP_USE_TLS': 'true',
1684+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
1685+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
1686+
'APPENGINE_SMTP_PORT': '587',
1687+
'APPENGINE_SMTP_USER': 'user',
1688+
'APPENGINE_SMTP_PASSWORD': 'password',
1689+
'APPENGINE_SMTP_USE_TLS': 'true',
16901690
}
16911691

16921692
to_list = ['to1@example.com', 'to2@example.com']
@@ -1718,9 +1718,9 @@ def testSendEmailViaSmtp_MultipleRecipients(self, mock_smtp):
17181718
def testSendEmailViaSmtp_HtmlBody(self, mock_smtp):
17191719
"""Tests that mail.send_mail handles HTML bodies correctly."""
17201720
environ = {
1721-
'USE_SMTP_MAIL_SERVICE': 'true',
1722-
'SMTP_HOST': 'smtp.example.com',
1723-
'SMTP_PORT': '587',
1721+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
1722+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
1723+
'APPENGINE_SMTP_PORT': '587',
17241724
}
17251725

17261726
text_body = 'This is the plain text body.'
@@ -1762,7 +1762,7 @@ def testSendEmailViaSmtp_HtmlBody(self, mock_smtp):
17621762
@mock.patch('smtplib.SMTP')
17631763
def testSendEmailViaSmtp_AttachmentsOnly(self, mock_smtp):
17641764
"""Tests sending an email with only attachments."""
1765-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1765+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
17661766
attachments = [
17671767
('one.txt', b'data1'),
17681768
('two.txt', b'data2'),
@@ -1798,7 +1798,7 @@ def testSendEmailViaSmtp_AttachmentsOnly(self, mock_smtp):
17981798
@mock.patch('smtplib.SMTP')
17991799
def testSendEmailViaSmtp_HtmlBodyOnly(self, mock_smtp):
18001800
"""Tests sending an email with only an HTML body."""
1801-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1801+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
18021802
html_body = '<h1>Just HTML</h1>'
18031803

18041804
with mock.patch.dict('os.environ', environ):
@@ -1835,7 +1835,7 @@ def testSendEmailViaSmtp_HtmlBodyOnly(self, mock_smtp):
18351835
@mock.patch('smtplib.SMTP')
18361836
def testSendEmailViaSmtp_WithAttachment(self, mock_smtp):
18371837
"""Tests that mail.send_mail handles a single attachment correctly."""
1838-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1838+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
18391839
attachment_data = b'This is attachment data.'
18401840

18411841
with mock.patch.dict('os.environ', environ):
@@ -1863,7 +1863,7 @@ def testSendEmailViaSmtp_WithAttachment(self, mock_smtp):
18631863
@mock.patch('smtplib.SMTP')
18641864
def testSendEmailViaSmtp_WithMultipleAttachments(self, mock_smtp):
18651865
"""Tests that mail.send_mail handles multiple attachments correctly."""
1866-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1866+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
18671867
attachments = [
18681868
('one.txt', b'data1'),
18691869
('two.txt', b'data2'),
@@ -1896,7 +1896,7 @@ def testSendEmailViaSmtp_WithMultipleAttachments(self, mock_smtp):
18961896
@mock.patch('smtplib.SMTP')
18971897
def testSendEmailViaSmtp_WithHtmlAndAttachment(self, mock_smtp):
18981898
"""Tests handling of both HTML body and attachments."""
1899-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1899+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
19001900

19011901
with mock.patch.dict('os.environ', environ):
19021902
mail.send_mail(
@@ -1928,7 +1928,7 @@ def testSendEmailViaSmtp_WithHtmlAndAttachment(self, mock_smtp):
19281928
@mock.patch('smtplib.SMTP')
19291929
def testSendEmailViaSmtp_WithReplyTo(self, mock_smtp):
19301930
"""Tests that the Reply-To header is handled correctly."""
1931-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1931+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
19321932

19331933
with mock.patch.dict('os.environ', environ):
19341934
mail.send_mail(
@@ -1946,7 +1946,7 @@ def testSendEmailViaSmtp_WithReplyTo(self, mock_smtp):
19461946
@mock.patch('smtplib.SMTP')
19471947
def testSendEmailViaSmtp_WithCustomHeaders(self, mock_smtp):
19481948
"""Tests that custom headers are handled correctly."""
1949-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1949+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
19501950
headers = {
19511951
'List-Id': 'some-list <list.example.com>',
19521952
'References': '<foo@bar.com>'
@@ -1969,7 +1969,7 @@ def testSendEmailViaSmtp_WithCustomHeaders(self, mock_smtp):
19691969
@mock.patch('smtplib.SMTP')
19701970
def testSendEmailViaSmtp_WithAttachmentContentId(self, mock_smtp):
19711971
"""Tests that attachments with Content-ID are handled correctly."""
1972-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
1972+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
19731973
attachment = mail.Attachment(
19741974
'image.png', b'image data', content_id='<image_id>')
19751975

@@ -1993,9 +1993,9 @@ def testSendEmailViaSmtp_WithAttachmentContentId(self, mock_smtp):
19931993
def testSendEmailViaSmtp_NoTls(self, mock_smtp):
19941994
"""Tests that TLS is not used when disabled."""
19951995
environ = {
1996-
'USE_SMTP_MAIL_SERVICE': 'true',
1997-
'SMTP_HOST': 'smtp.example.com',
1998-
'SMTP_USE_TLS': 'false',
1996+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
1997+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
1998+
'APPENGINE_SMTP_USE_TLS': 'false',
19991999
}
20002000

20012001
with mock.patch.dict('os.environ', environ):
@@ -2012,8 +2012,8 @@ def testSendEmailViaSmtp_NoTls(self, mock_smtp):
20122012
def testSendEmailViaSmtp_NoAuth(self, mock_smtp):
20132013
"""Tests that login is not attempted when credentials are not provided."""
20142014
environ = {
2015-
'USE_SMTP_MAIL_SERVICE': 'true',
2016-
'SMTP_HOST': 'smtp.example.com',
2015+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
2016+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
20172017
}
20182018

20192019
with mock.patch.dict('os.environ', environ):
@@ -2031,9 +2031,9 @@ def testSendAdminEmailViaSmtp(self, mock_smtp):
20312031
"""Tests that admin emails are sent to the list in the env var."""
20322032
admin_list = 'admin1@example.com,admin2@example.com'
20332033
environ = {
2034-
'USE_SMTP_MAIL_SERVICE': 'true',
2035-
'SMTP_HOST': 'smtp.example.com',
2036-
'ADMIN_EMAIL_RECIPIENTS': admin_list,
2034+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
2035+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
2036+
'APPENGINE_ADMIN_EMAIL_RECIPIENTS': admin_list,
20372037
}
20382038

20392039
with mock.patch.dict('os.environ', environ):
@@ -2049,7 +2049,7 @@ def testSendAdminEmailViaSmtp(self, mock_smtp):
20492049
@mock.patch('smtplib.SMTP')
20502050
def testSendEmailViaSmtp_AmpHtmlBody(self, mock_smtp):
20512051
"""Tests that mail.send_mail handles AMP HTML bodies correctly."""
2052-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
2052+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
20532053
amp_html_body = '<html><body>AMP for Email is awesome!</body></html>'
20542054

20552055
with mock.patch.dict('os.environ', environ):
@@ -2090,8 +2090,8 @@ def testInvalidAttachmentType(self):
20902090
def testSendAdminEmailViaSmtp_NoRecipients(self, mock_smtp):
20912091
"""Tests that an error is raised when no admin recipients are specified."""
20922092
environ = {
2093-
'USE_SMTP_MAIL_SERVICE': 'true',
2094-
'SMTP_HOST': 'smtp.example.com',
2093+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
2094+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
20952095
}
20962096

20972097
with mock.patch.dict('os.environ', environ):
@@ -2105,10 +2105,10 @@ def testSendAdminEmailViaSmtp_NoRecipients(self, mock_smtp):
21052105
def testSendEmailViaSmtp_AuthenticationError(self, mock_smtp):
21062106
"""Tests that an authentication error is handled correctly."""
21072107
environ = {
2108-
'USE_SMTP_MAIL_SERVICE': 'true',
2109-
'SMTP_HOST': 'smtp.example.com',
2110-
'SMTP_USER': 'user',
2111-
'SMTP_PASSWORD': 'password',
2108+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
2109+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
2110+
'APPENGINE_SMTP_USER': 'user',
2111+
'APPENGINE_SMTP_PASSWORD': 'password',
21122112
}
21132113

21142114
instance = mock_smtp.return_value.__enter__.return_value
@@ -2126,8 +2126,8 @@ def testSendEmailViaSmtp_AuthenticationError(self, mock_smtp):
21262126
def testSendEmailViaSmtp_ConnectionError(self, mock_smtp):
21272127
"""Tests that a connection error is handled correctly."""
21282128
environ = {
2129-
'USE_SMTP_MAIL_SERVICE': 'true',
2130-
'SMTP_HOST': 'smtp.example.com',
2129+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
2130+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
21312131
}
21322132

21332133
mock_smtp.side_effect = smtplib.SMTPConnectError(550, 'Connection refused')
@@ -2144,8 +2144,8 @@ def testSendEmailViaSmtp_ConnectionError(self, mock_smtp):
21442144
def testSendEmailViaSmtp_ConnectionError(self, mock_smtp):
21452145
"""Tests that a connection error is handled correctly."""
21462146
environ = {
2147-
'USE_SMTP_MAIL_SERVICE': 'true',
2148-
'SMTP_HOST': 'smtp.example.com',
2147+
'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true',
2148+
'APPENGINE_SMTP_HOST': 'smtp.example.com',
21492149
}
21502150

21512151
mock_smtp.side_effect = smtplib.SMTPConnectError(550, 'Connection refused')
@@ -2161,7 +2161,7 @@ def testSendEmailViaSmtp_ConnectionError(self, mock_smtp):
21612161
@mock.patch('smtplib.SMTP')
21622162
def testSendEmailViaSmtp_WithUnicode(self, mock_smtp):
21632163
"""Tests that unicode characters are handled correctly."""
2164-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
2164+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
21652165

21662166
sender = u'J\xe9r\xe9my <sender@example.com>'
21672167
subject = u'Un sujet avec des caract\xe8res sp\xe9ciaux'
@@ -2191,7 +2191,7 @@ def testSendEmailViaSmtp_WithUnicode(self, mock_smtp):
21912191
@mock.patch('smtplib.SMTP')
21922192
def testSendEmailViaSmtp_WithAmpHtml(self, mock_smtp):
21932193
"""Tests that AMP HTML is handled correctly."""
2194-
environ = {'USE_SMTP_MAIL_SERVICE': 'true', 'SMTP_HOST': 'smtp.example.com'}
2194+
environ = {'APPENGINE_USE_SMTP_MAIL_SERVICE': 'true', 'APPENGINE_SMTP_HOST': 'smtp.example.com'}
21952195

21962196
text_body = 'Plain text'
21972197
html_body = '<h1>HTML</h1>'

0 commit comments

Comments
 (0)