Add email send reasons

This commit is contained in:
rubenwardy 2020-12-05 19:52:02 +00:00
parent 3efda30b98
commit 5fe3b0b459
7 changed files with 42 additions and 17 deletions

@ -23,7 +23,7 @@ from wtforms.validators import *
from app.markdown import render_markdown from app.markdown import render_markdown
from app.models import * 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 app.utils import rank_required, addAuditLog
from . import bp from . import bp
@ -55,7 +55,7 @@ def send_single_email():
text = form.text.data text = form.text.data
html = render_markdown(text) 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 redirect(url_for("tasks.check", id=task.id, r=next_url))
return render_template("admin/send_email.html", form=form, user=user) return render_template("admin/send_email.html", form=form, user=user)
@ -72,7 +72,7 @@ def send_bulk_email():
text = form.text.data text = form.text.data
html = render_markdown(text) html = render_markdown(text)
for user in User.query.filter(User.email != None).all(): 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")) return redirect(url_for("admin.admin_page"))

@ -23,7 +23,7 @@ from wtforms import *
from wtforms.validators import * from wtforms.validators import *
from app.models 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 app.utils import randomString, make_flask_login_password, is_safe_url, check_password_hash, addAuditLog
from passlib.pwd import genphrase from passlib.pwd import genphrase
@ -106,7 +106,7 @@ def register():
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first() user = User.query.filter_by(email=form.email.data).first()
if user: 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)) "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: else:
user = User(form.username.data, False, form.email.data, make_flask_login_password(form.password.data)) 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) sendVerifyEmail.delay(form.email.data, token)
else: else:
sendEmailRaw.delay([email], "Unable to find account", """ send_anon_email.delay([email], "Unable to find account", """
<p> <p>
We were unable to perform the password reset as we could not find an account We were unable to perform the password reset as we could not find an account
associated with this email. associated with this email.

@ -1,6 +1,6 @@
import logging import logging
from app.tasks.emails import sendEmailRaw from app.tasks.emails import send_user_email
def _has_newline(line): def _has_newline(line):
@ -82,7 +82,7 @@ class FlaskMailHandler(logging.Handler):
text = self.format(record) if self.formatter else None text = self.format(record) if self.formatter else None
html = self.html_formatter.format(record) if self.html_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): def register_mail_error_handler(app, mailer):

@ -41,17 +41,30 @@ def sendVerifyEmail(newEmail, token):
msg.html = render_template("emails/verify.html", token=token) msg.html = render_template("emails/verify.html", token=token)
mail.send(msg) mail.send(msg)
@celery.task() @celery.task()
def sendEmailRaw(to, subject, text, html=None): def send_email_with_reason(to, subject, text, html, reason):
from flask_mail import Message from flask_mail import Message
msg = Message(subject, recipients=to) msg = Message(subject, recipients=to)
msg.body = text msg.body = text
html = html or 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) 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): def sendNotificationEmail(notification):
msg = Message(notification.title, recipients=[notification.user.email]) msg = Message(notification.title, recipients=[notification.user.email])

@ -56,7 +56,14 @@
{% endblock %} {% endblock %}
<div style="margin-top: 3em;font-size: 80%;color: #666;"> <div style="margin-top: 3em;font-size: 80%;color: #666;">
ContentDB &copy; rubenwardy <p>
{% block footer %}
{{ reason }}
{% endblock %}
</p>
<p>
ContentDB &copy; rubenwardy
</p>
</div> </div>
</div> </div>
</div> </div>

@ -21,10 +21,11 @@
</a> </a>
</p> </p>
<p><small> {% endblock %}
<a href="{{ abs_url_for('users.email_notifications', username=notification.user.username) }}">
{{ _("Edit notification settings") }} {% block footer %}
</a> You are receiving this email because you are a registered user of ContentDB, and have email notifications enabled.<br>
</small></p> <a href="{{ abs_url_for('users.email_notifications', username=notification.user.username) }}">
{{ _("Manage your preferences") }}
</a>
{% endblock %} {% endblock %}

@ -25,3 +25,7 @@
<p> <p>
{% endblock %} {% endblock %}
{% block footer %}
You are receiving this email because someone (hopefully you) entered your email address as a user's email.
{% endblock %}