tildetown-admin/ttadmin/common/mailing.py

36 lines
815 B
Python
Raw Normal View History

2017-01-20 08:40:18 +00:00
import logging
import requests
2017-01-21 06:53:17 +00:00
from django.conf import settings
2017-01-20 08:40:18 +00:00
logger = logging.getLogger()
2018-09-06 02:33:07 +00:00
ADMIN_NAME='vilmibm'
EXTERNAL_FROM='root@tilde.town'
REPLY_TO='tildetown@protonmail.ch'
2017-01-20 08:40:18 +00:00
def send_email(to, body, subject='a message from tilde.town'):
2017-01-20 08:40:18 +00:00
"""Sends an email using mailgun. Logs on failure."""
response = requests.post(
2017-01-21 06:53:17 +00:00
settings.MAILGUN_URL,
auth=('api', settings.MAILGUN_KEY),
2017-01-20 08:40:18 +00:00
data={
'from': EXTERNAL_FROM,
'h:Reply-To': REPLY_TO,
2017-01-20 08:40:18 +00:00
'to': to,
'subject': subject,
'text': body
}
)
2017-01-21 08:34:17 +00:00
success = response.status_code == 200
if not success:
2017-01-21 06:53:17 +00:00
logger.error('{}: failed to send email "{}" to {}'.format(
response.status_code,
subject,
to))
2017-01-21 08:34:17 +00:00
return success