From f56e12a0538f1484dd88bd737bcf9f95a6d2859d Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 3 Jul 2019 20:41:40 +0000 Subject: [PATCH 1/3] switch away from mailgun and to external SMTP --- ttadmin/common/mailing.py | 43 ++++++++++++++++----------------------- ttadmin/settings.py | 7 ++++--- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/ttadmin/common/mailing.py b/ttadmin/common/mailing.py index ccf00b2..dc3a9d0 100644 --- a/ttadmin/common/mailing.py +++ b/ttadmin/common/mailing.py @@ -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 diff --git a/ttadmin/settings.py b/ttadmin/settings.py index b3c4091..6921c7d 100644 --- a/ttadmin/settings.py +++ b/ttadmin/settings.py @@ -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" From b33c244d7b2d37a8d18ef9f3e470bfbcce4dc46a Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 3 Jul 2019 20:41:50 +0000 Subject: [PATCH 2/3] allow referral to be blank --- ttadmin/users/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttadmin/users/models.py b/ttadmin/users/models.py index f406e17..c731f0a 100644 --- a/ttadmin/users/models.py +++ b/ttadmin/users/models.py @@ -47,7 +47,7 @@ class Townie(User): reasons = TextField(blank=True, null=False, default='') plans = TextField(blank=True, null=False, default='') socials = TextField(blank=True, null=False, default='') - referral = CharField(max_length=100, null=True) + referral = CharField(max_length=100, null=True, blank=True) displayname = CharField(max_length=100, blank=False, null=False) @property From 627ccf6e5575f475e51dc03547e3b09c48e338ad Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 3 Jul 2019 21:02:50 +0000 Subject: [PATCH 3/3] no longer need requests --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 9e793fe..c5f07b6 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,6 @@ setup( install_requires = ['Django==1.10.2', 'sshpubkeys==2.2.0', 'psycopg2==2.7.6.1', - 'requests==2.12.5', 'gunicorn==19.6.0', 'Mastodon.py==1.1.1', 'tweepy==3.5.0'],