From b63f19f415c61de0e21eb94e3ebb0d7119cbef98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sun, 29 Jan 2017 11:44:22 +0200 Subject: [PATCH] Simplify: remove djmail and django-ses-backend dependencies. --- README.md | 26 +++++++++++--------------- hc/api/tests/__init__.py | 3 +++ hc/lib/emails.py | 35 +++++++++++++++++++++++++++++++---- hc/local_settings.py.example | 7 +++++++ hc/settings.py | 3 --- requirements.txt | 2 -- 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index dc4c6fc7..f8fafdb7 100644 --- a/README.md +++ b/README.md @@ -135,21 +135,17 @@ configuration from environment variables like so: ## Sending Emails healthchecks must be able to send email messages, so it can send out login -links and alerts to users. You will likely need to tweak email configuration -before emails will work. healthchecks uses -[djmail](http://bameda.github.io/djmail/) for sending emails asynchronously. -Djmail is a BSD Licensed, simple and nonobstructive django email middleware. -It can be configured to use any regular Django email backend behind the -scenes. For example, the healthchecks.io site uses -[django-ses-backend](https://github.com/piotrbulinski/django-ses-backend/) -and the email configuration in `hc/local_settings.py` looks as follows: - - DEFAULT_FROM_EMAIL = 'noreply@my-monitoring-project.com' - DJMAIL_REAL_BACKEND = 'django_ses_backend.SESBackend' - AWS_SES_ACCESS_KEY_ID = "put-access-key-here" - AWS_SES_SECRET_ACCESS_KEY = "put-secret-access-key-here" - AWS_SES_REGION_NAME = 'us-east-1' - AWS_SES_REGION_ENDPOINT = 'email.us-east-1.amazonaws.com' +links and alerts to users. Put your SMTP server configuration in +`hc/local_settings.py` like so: + + EMAIL_HOST = "your-smtp-server-here.com" + EMAIL_PORT = 587 + EMAIL_HOST_USER = "username" + EMAIL_HOST_PASSWORD = "password" + EMAIL_USE_TLS = True + +For more information, have a look at Django documentation, +[Sending Email](https://docs.djangoproject.com/en/1.10/topics/email/) section. ## Sending Status Notifications diff --git a/hc/api/tests/__init__.py b/hc/api/tests/__init__.py index ad3f004f..b5ec7a15 100644 --- a/hc/api/tests/__init__.py +++ b/hc/api/tests/__init__.py @@ -9,4 +9,7 @@ class CustomRunner(DiscoverRunner): settings.PASSWORD_HASHERS = \ ('django.contrib.auth.hashers.MD5PasswordHasher', ) + # Send emails synchronously + settings.BLOCKING_EMAILS = True + super(CustomRunner, self).__init__(*args, **kwargs) diff --git a/hc/lib/emails.py b/hc/lib/emails.py index a6b4b5a6..6ea3b591 100644 --- a/hc/lib/emails.py +++ b/hc/lib/emails.py @@ -1,11 +1,38 @@ +from threading import Thread + from django.conf import settings -from djmail.template_mail import TemplateMail +from django.core.mail import EmailMultiAlternatives +from django.template.loader import render_to_string as render + + +class EmailThread(Thread): + def __init__(self, name, to, ctx): + Thread.__init__(self) + self.name = name + self.to = to + self.ctx = ctx + + def run(self): + self.ctx["SITE_ROOT"] = settings.SITE_ROOT + + subject = render('emails/%s-subject.html' % self.name, self.ctx) + subject = subject.strip() + + text = render('emails/%s-body-text.html' % self.name, self.ctx) + html = render('emails/%s-body-html.html' % self.name, self.ctx) + + msg = EmailMultiAlternatives(subject, text, to=(self.to, )) + + msg.attach_alternative(html, "text/html") + msg.send() def send(name, to, ctx): - o = TemplateMail(name) - ctx["SITE_ROOT"] = settings.SITE_ROOT - o.send(to, ctx) + t = EmailThread(name, to, ctx) + if hasattr(settings, "BLOCKING_EMAILS"): + t.run() + else: + t.start() def login(to, ctx): diff --git a/hc/local_settings.py.example b/hc/local_settings.py.example index bd90d72c..849e9ae1 100644 --- a/hc/local_settings.py.example +++ b/hc/local_settings.py.example @@ -23,3 +23,10 @@ # 'TEST': {'CHARSET': 'UTF8'} # } # } + +# Email +# EMAIL_HOST = "your-smtp-server-here.com" +# EMAIL_PORT = 587 +# EMAIL_HOST_USER = "username" +# EMAIL_HOST_PASSWORD = "password" +# EMAIL_USE_TLS = True diff --git a/hc/settings.py b/hc/settings.py index 22342e20..e41f24bf 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -32,7 +32,6 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'compressor', - 'djmail', 'hc.accounts', 'hc.api', @@ -134,8 +133,6 @@ STATICFILES_FINDERS = ( ) COMPRESS_OFFLINE = True -EMAIL_BACKEND = "djmail.backends.default.EmailBackend" - # Discord integration -- override these in local_settings DISCORD_CLIENT_ID = None DISCORD_CLIENT_SECRET = None diff --git a/requirements.txt b/requirements.txt index 6ee11c62..3d669c09 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,6 @@ croniter -django-ses-backend==0.1.1 Django==1.10.5 django_compressor==2.1 -djmail==0.11.0 psycopg2==2.6.2 pytz==2016.7 requests==2.9.1