2017-01-20 08:40:18 +00:00
|
|
|
import logging
|
2019-07-03 20:41:40 +00:00
|
|
|
from smtplib import SMTP_SSL, SMTPException
|
|
|
|
from email.message import EmailMessage
|
2017-01-20 08:40:18 +00:00
|
|
|
|
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:25:00 +00:00
|
|
|
def send_email(to, body, subject='a message from tilde.town'):
|
2019-07-03 20:41:40 +00:00
|
|
|
"""Sends an email using external SMTP. Logs on failure."""
|
|
|
|
em = EmailMessage()
|
|
|
|
em['Subject'] = subject
|
|
|
|
em['From'] = 'root@tilde.town'
|
|
|
|
em['To'] = to
|
|
|
|
em.set_content(body)
|
|
|
|
try:
|
|
|
|
with SMTP_SSL(port=settings.SMTP_PORT, host=settings.SMTP_HOST) as smtp:
|
|
|
|
smtp.login('root@tilde.town', settings.SMTP_PASSWORD)
|
|
|
|
smtp.send_message(em)
|
|
|
|
smtp.quit()
|
|
|
|
except SMTPException as e:
|
|
|
|
logger.error(f'failed to send email "{subject}" to {to}: {e}')
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|