Adds email templating

This commit is contained in:
Mouse Reeve 2021-03-21 08:54:57 -07:00
parent 5b8bd9ad44
commit 685da1f5b8
4 changed files with 28 additions and 15 deletions

View file

@ -1,5 +1,6 @@
""" send emails """ """ send emails """
from django.core.mail import send_mail from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from bookwyrm import models from bookwyrm import models
from bookwyrm.tasks import app from bookwyrm.tasks import app
@ -7,21 +8,27 @@ from bookwyrm.tasks import app
def password_reset_email(reset_code): def password_reset_email(reset_code):
""" generate a password reset email """ """ generate a password reset email """
site = models.SiteSettings.get() site = models.SiteSettings.objects.get()
send_email.delay( data = {
reset_code.user.email, "site_name": site.name,
"Reset your password on %s" % site.name, "reset_link": reset_code.link,
"Your password reset link: %s" % reset_code.link, }
) send_email.delay(reset_code.user.email, "password_reset", data)
@app.task @app.task
def send_email(recipient, subject, message): def send_email(recipient, message_name, data):
""" use a task to send the email """ """ use a task to send the email """
send_mail( subject = get_template(
subject, "email/{}/subject.html".format(message_name)
message, ).render(data).strip()
None, # sender will be the config default html_content = get_template(
[recipient], "email/{}/html_content.html".format(message_name)
fail_silently=False, ).render(data).strip()
) text_content = get_template(
"email/{}/text_content.html".format(message_name)
).render(data).strip()
email = EmailMultiAlternatives(subject, text_content, None, [recipient])
email.attach_alternative(html_content, "text/html")
email.send()

View file

@ -0,0 +1,2 @@
{% load i18n %}
{% blocktrans %}Your password reset link is: {{ reset_link }}{% endblocktrans %}

View file

@ -0,0 +1,2 @@
{% load i18n %}
{% blocktrans %}Reset your password on {{ site_name }}{% endblocktrans %}

View file

@ -0,0 +1,2 @@
{% load i18n %}
{% blocktrans %}Your password reset link is: {{ reset_link }}{% endblocktrans %}