Skip to content

Commit

Permalink
Allow connecting to server with SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
PauloPhagula committed Jan 13, 2017
1 parent 4e8028f commit c3f1436
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=smtp_username
SMTP_PASSWORD=smtp_password
SMTP_USE_TLS=True
# Only one should be True
SMTP_USE_TLS=False
SMTP_USE_SSL=True
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
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.2.0 - 2017-01-13

- Allow connecting to server with SSL

## v0.1.1 - 2017-01-13

- Replace call to dict.iteritems which is Python3 by six.iteritems
Expand Down
6 changes: 5 additions & 1 deletion send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,20 @@ def send_mail(to, subject, message, is_html=False, cc=None, bcc=None,
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)
use_ssl = kwargs.get('use_ssl', False) or os.getenv('SMTP_USE_SSL', False)

if six.PY2:
password = six.binary_type(password)

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

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

try:
# this doesn't support `with` statement so we do `close` the old way.
mail_server = smtplib.SMTP(host, port)
mail_server = smtplib.SMTP_SSL(host, port) if use_ssl else smtplib.SMTP(host, port)
mail_server.ehlo()
if use_tls:
mail_server.starttls()
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.1',
version='0.2.0',
description='Simple email sending module for use in ETL/reporting script.',
long_description=LONG_DESCRIPTION,
url='https://github.com/dareenzo/send_mail',
Expand Down

0 comments on commit c3f1436

Please sign in to comment.