Skip to content

Commit

Permalink
Improve SMTP_USE_SSL parsing
Browse files Browse the repository at this point in the history
- Replace call to dict.iteritems which is Python3 by six.iteritems
- We put and get env. vars as strings, so parse SMTP_USE_SSL to bool
  before using
- Improve cheap_dot_env method in test code
  • Loading branch information
PauloPhagula committed Jan 13, 2017
1 parent acbf2a7 commit 4e8028f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=smtp_username
SMTP_PASSWORD=smtp_password
SMTP_USE_TLS=True
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
For a complete view of all the releases, visit the releases page on GitHub:
[https://github.com/dareenzo/send_mail/releases](https://github.com/dareenzo/send_mail/releases)

## v0.1.1 - 2017-01-13

- Replace call to dict.iteritems which is Python3 by six.iteritems
- We put and get env. vars as strings, so parse SMTP_USE_SSL to bool before using
- Improve cheap_dot_env method in test code


## v0.1.0 - 2017-01-12

- Initial release
9 changes: 6 additions & 3 deletions send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def send_mail(to, subject, message, is_html=False, cc=None, bcc=None,
raise

if custom_headers:
for k, v in custom_headers.iteritems():
for k, v in six.iteritems(custom_headers):
mail.add_header(k, v)

all_destinations = []
Expand All @@ -181,15 +181,18 @@ def send_mail(to, subject, message, is_html=False, cc=None, bcc=None,
if bcc:
all_destinations.extend(bcc)


host = kwargs.get('host', None) or os.getenv('SMTP_HOST')
port = kwargs.get('port', None) or os.getenv('SMTP_PORT')
port = int(port)
username = kwargs.get('username', None) or os.getenv('SMTP_USERNAME')
password = kwargs.get('password', None) or os.getenv('SMTP_PASSWORD')
use_tls = kwargs.get('use_tls', False) or os.getenv('SMTP_USE_TLS', False)

if six.PY2:
password = six.binary_type(password)
use_tls = kwargs.get('use_tls', None) or os.getenv('SMTP_USE_TLS')

if use_tls in ("False", "false"):
use_tls = False

try:
# this doesn't support `with` statement so we do `close` the old way.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def file_get_contents(filename):

setup(
name='send_mail',
version='0.1.0',
version='0.1.1',
description='Simple email sending module for use in ETL/reporting script.',
long_description=LONG_DESCRIPTION,
url='https://github.com/dareenzo/send_mail',
Expand Down
6 changes: 4 additions & 2 deletions test_send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def cheap_dot_env(dot_env_path):
line = line.strip()
if len(line) == 0 or line.startswith(';') or line.startswith('#'):
continue
var = line.replace('"', '')
var = line.strip().split('=')
if len(var) == 2:
os.putenv(var[0], var[1].strip())
os.environ[var[0]] = var[1].strip()
key = var[0]
value = var[1].replace('"', '').strip()
os.environ[key] = value
else:
raise Exception('no dot env file')

Expand Down

0 comments on commit 4e8028f

Please sign in to comment.