From 5fe3b0b459e6c8f46e8c43af385254e7229fa51f Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 5 Dec 2020 19:52:02 +0000 Subject: [PATCH] Add email send reasons --- app/blueprints/admin/email.py | 6 +++--- app/blueprints/users/account.py | 6 +++--- app/maillogger.py | 4 ++-- app/tasks/emails.py | 17 +++++++++++++++-- app/templates/emails/base.html | 9 ++++++++- app/templates/emails/notification.html | 13 +++++++------ app/templates/emails/verify.html | 4 ++++ 7 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/blueprints/admin/email.py b/app/blueprints/admin/email.py index a191510..e3c11ac 100644 --- a/app/blueprints/admin/email.py +++ b/app/blueprints/admin/email.py @@ -23,7 +23,7 @@ from wtforms.validators import * from app.markdown import render_markdown from app.models import * -from app.tasks.emails import sendEmailRaw +from app.tasks.emails import send_user_email from app.utils import rank_required, addAuditLog from . import bp @@ -55,7 +55,7 @@ def send_single_email(): text = form.text.data html = render_markdown(text) - task = sendEmailRaw.delay([user.email], form.subject.data, text, html) + task = send_user_email.delay([user.email], form.subject.data, text, html) return redirect(url_for("tasks.check", id=task.id, r=next_url)) return render_template("admin/send_email.html", form=form, user=user) @@ -72,7 +72,7 @@ def send_bulk_email(): text = form.text.data html = render_markdown(text) for user in User.query.filter(User.email != None).all(): - sendEmailRaw.delay([user.email], form.subject.data, text, html) + send_user_email.delay([user.email], form.subject.data, text, html) return redirect(url_for("admin.admin_page")) diff --git a/app/blueprints/users/account.py b/app/blueprints/users/account.py index 12eb0a7..a1bb6cd 100644 --- a/app/blueprints/users/account.py +++ b/app/blueprints/users/account.py @@ -23,7 +23,7 @@ from wtforms import * from wtforms.validators import * from app.models import * -from app.tasks.emails import sendVerifyEmail, sendEmailRaw +from app.tasks.emails import sendVerifyEmail, send_anon_email from app.utils import randomString, make_flask_login_password, is_safe_url, check_password_hash, addAuditLog from passlib.pwd import genphrase @@ -106,7 +106,7 @@ def register(): if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: - sendEmailRaw.delay([form.email.data], "Email already in use", + send_anon_email.delay([form.email.data], "Email already in use", "We were unable to create the account as the email is already in use by {}. Try a different email address.".format(user.display_name)) else: user = User(form.username.data, False, form.email.data, make_flask_login_password(form.password.data)) @@ -159,7 +159,7 @@ def forgot_password(): sendVerifyEmail.delay(form.email.data, token) else: - sendEmailRaw.delay([email], "Unable to find account", """ + send_anon_email.delay([email], "Unable to find account", """

We were unable to perform the password reset as we could not find an account associated with this email. diff --git a/app/maillogger.py b/app/maillogger.py index 8d9c698..30b4f1f 100644 --- a/app/maillogger.py +++ b/app/maillogger.py @@ -1,6 +1,6 @@ import logging -from app.tasks.emails import sendEmailRaw +from app.tasks.emails import send_user_email def _has_newline(line): @@ -82,7 +82,7 @@ class FlaskMailHandler(logging.Handler): text = self.format(record) if self.formatter else None html = self.html_formatter.format(record) if self.html_formatter else None - sendEmailRaw.delay(self.send_to, self.getSubject(record), text, html) + send_user_email.delay(self.send_to, self.getSubject(record), text, html) def register_mail_error_handler(app, mailer): diff --git a/app/tasks/emails.py b/app/tasks/emails.py index 0b4967d..72a2aef 100644 --- a/app/tasks/emails.py +++ b/app/tasks/emails.py @@ -41,17 +41,30 @@ def sendVerifyEmail(newEmail, token): msg.html = render_template("emails/verify.html", token=token) mail.send(msg) + @celery.task() -def sendEmailRaw(to, subject, text, html=None): +def send_email_with_reason(to, subject, text, html, reason): from flask_mail import Message msg = Message(subject, recipients=to) msg.body = text html = html or text - msg.html = render_template("emails/base.html", subject=subject, content=html) + msg.html = render_template("emails/base.html", subject=subject, content=html, reason=reason) mail.send(msg) +@celery.task() +def send_user_email(to, subject, text, html=None): + return send_email_with_reason(to, subject, text, html, + "You are receiving this email because you are a registered user of ContentDB.") + + +@celery.task() +def send_anon_email(to, subject, text, html=None): + return send_email_with_reason(to, subject, text, html, + "You are receiving this email because someone (hopefully you) entered your email address as a user's email.") + + def sendNotificationEmail(notification): msg = Message(notification.title, recipients=[notification.user.email]) diff --git a/app/templates/emails/base.html b/app/templates/emails/base.html index ebdf85a..48cf969 100644 --- a/app/templates/emails/base.html +++ b/app/templates/emails/base.html @@ -56,7 +56,14 @@ {% endblock %}

- ContentDB © rubenwardy +

+ {% block footer %} + {{ reason }} + {% endblock %} +

+

+ ContentDB © rubenwardy +

diff --git a/app/templates/emails/notification.html b/app/templates/emails/notification.html index 1ca295e..0b2d59b 100644 --- a/app/templates/emails/notification.html +++ b/app/templates/emails/notification.html @@ -21,10 +21,11 @@

-

- - {{ _("Edit notification settings") }} - -

- +{% endblock %} + +{% block footer %} + You are receiving this email because you are a registered user of ContentDB, and have email notifications enabled.
+ + {{ _("Manage your preferences") }} + {% endblock %} diff --git a/app/templates/emails/verify.html b/app/templates/emails/verify.html index ca9298e..84507b2 100644 --- a/app/templates/emails/verify.html +++ b/app/templates/emails/verify.html @@ -25,3 +25,7 @@

{% endblock %} + +{% block footer %} + You are receiving this email because someone (hopefully you) entered your email address as a user's email. +{% endblock %}