switch away from mailgun and to external SMTP

zoho
vilmibm 2019-07-03 20:41:40 +00:00
parent a8917a05c3
commit f56e12a053
2 changed files with 21 additions and 29 deletions

View File

@ -1,35 +1,26 @@
import logging
import requests
from smtplib import SMTP_SSL, SMTPException
from email.message import EmailMessage
from django.conf import settings
logger = logging.getLogger()
ADMIN_NAME='vilmibm'
EXTERNAL_FROM='root@tilde.town'
REPLY_TO='tildetown@protonmail.ch'
def send_email(to, body, subject='a message from tilde.town'):
"""Sends an email using mailgun. Logs on failure."""
response = requests.post(
settings.MAILGUN_URL,
auth=('api', settings.MAILGUN_KEY),
data={
'from': EXTERNAL_FROM,
'h:Reply-To': REPLY_TO,
'to': to,
'subject': subject,
'text': body
}
)
"""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
success = response.status_code == 200
if not success:
logger.error('{}: failed to send email "{}" to {}'.format(
response.status_code,
subject,
to))
return success
return True

View File

@ -6,7 +6,7 @@ To run this For Real, you'll want to:
* set a different SECRET_KEY
* change the password for the database or delete the password and use ident
* change DEBUG to False
* set mailgun api info
* set smtp password
"""
import os
@ -101,8 +101,9 @@ STATIC_URL = '/static/'
# Not used during local development, but used in staging+live environments
STATIC_ROOT = 'static'
MAILGUN_URL = "OVERWRITE THIS"
MAILGUN_KEY = "OVERWRITE THIS"
SMTP_PORT=465
SMTP_HOST="smtp.zoho.com"
SMTP_PASSWORD="OVERWRITE THIS"
# Mastodon credentials
MASTO_CLIENT_ID = "OVERWRITE THIS"