From 7d2417814893a083994d30e60b4003144a69987a Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Fri, 20 Jan 2017 00:40:18 -0800 Subject: [PATCH] totally untested email function --- setup.py | 3 ++- ttadmin/common/mailing.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ttadmin/common/mailing.py diff --git a/setup.py b/setup.py index 494e73d..68f1f0c 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,7 @@ setup( packages=['ttadmin'], install_requires = ['Django==1.10.2', 'sshpubkeys==2.2.0', - 'psycopg2==2.6.2',], + 'psycopg2==2.6.2', + 'requests==2.12.5'], include_package_data = True, ) diff --git a/ttadmin/common/mailing.py b/ttadmin/common/mailing.py new file mode 100644 index 0000000..faab1c1 --- /dev/null +++ b/ttadmin/common/mailing.py @@ -0,0 +1,25 @@ +import logging + +import requests + +from django.conf.settings import MAILGUN_URL, MAILGUN_KEY + +logger = logging.getLogger() + +FROM='root@tilde.town' + +def send_email(to, body, subject='a message from tilde.town', frum=FROM,): + """Sends an email using mailgun. Logs on failure.""" + response = requests.post( + MAILGUN_URL, + auth=('api', MAILGUN_KEY), + data={ + 'from': frum, + 'to': to, + 'subject': subject, + 'text': body + } + ) + + if response.status_code != 200: + logger.error('failed to send email "{}" to {}'.format(subject, to))