From 05adbace245b4429a38d86fab4041d863a6e8bd5 Mon Sep 17 00:00:00 2001 From: NaphalSec Date: Fri, 30 Dec 2022 18:03:11 -0500 Subject: [PATCH] email templates (#328) --- core/templatetags/__init__.py | 0 core/templatetags/mail_tags.py | 25 ++++ templates/emails/_body_content.html | 3 + templates/emails/_button.html | 17 +++ templates/emails/_footer.html | 7 ++ templates/emails/account_new.html | 27 +++++ templates/emails/base.html | 163 +++++++++++++++++++++++++++ templates/emails/password_reset.html | 27 +++++ users/models/password_reset.py | 16 +++ 9 files changed, 285 insertions(+) create mode 100644 core/templatetags/__init__.py create mode 100644 core/templatetags/mail_tags.py create mode 100644 templates/emails/_body_content.html create mode 100644 templates/emails/_button.html create mode 100644 templates/emails/_footer.html create mode 100644 templates/emails/account_new.html create mode 100644 templates/emails/base.html create mode 100644 templates/emails/password_reset.html diff --git a/core/templatetags/__init__.py b/core/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/templatetags/mail_tags.py b/core/templatetags/mail_tags.py new file mode 100644 index 0000000..60cbc8b --- /dev/null +++ b/core/templatetags/mail_tags.py @@ -0,0 +1,25 @@ +from django.template import Library, Template + +register = Library() + + +@register.inclusion_tag("emails/_body_content.html", takes_context=True) +def email_body_content(context, content): + template = Template(content) + return {"content": template.render(context)} + + +@register.inclusion_tag("emails/_button.html", takes_context=True) +def email_button(context, button_text, button_link): + text_template = Template(button_text) + link_template = Template(button_link) + return { + "button_text": text_template.render(context), + "button_link": link_template.render(context), + } + + +@register.inclusion_tag("emails/_footer.html", takes_context=True) +def email_footer(context, content): + template = Template(content) + return {"content": template.render(context)} diff --git a/templates/emails/_body_content.html b/templates/emails/_body_content.html new file mode 100644 index 0000000..c41af1b --- /dev/null +++ b/templates/emails/_body_content.html @@ -0,0 +1,3 @@ +{% autoescape off %} +

{{ content }}

+{% endautoescape %} diff --git a/templates/emails/_button.html b/templates/emails/_button.html new file mode 100644 index 0000000..4d14181 --- /dev/null +++ b/templates/emails/_button.html @@ -0,0 +1,17 @@ +{% autoescape off %} + + + + + + + +{% endautoescape %} diff --git a/templates/emails/_footer.html b/templates/emails/_footer.html new file mode 100644 index 0000000..13c2c92 --- /dev/null +++ b/templates/emails/_footer.html @@ -0,0 +1,7 @@ +{% autoescape off %} + + + {{ content }} + + +{% endautoescape %} diff --git a/templates/emails/account_new.html b/templates/emails/account_new.html new file mode 100644 index 0000000..f0f36c3 --- /dev/null +++ b/templates/emails/account_new.html @@ -0,0 +1,27 @@ +{% extends "emails/base.html" %} +{% load mail_tags %} +{% autoescape off %} +{% block title %}{{settings.MAIN_DOMAIN}} - New Account Created{% endblock title %} +{% block subtitle %}{{settings.MAIN_DOMAIN}} - New Account Created{% endblock subtitle %} + +{% block body_greeting %} + {% email_body_content "Hi there," %} +{% endblock body_greeting %} + +{% block body_content %} + {% email_body_content "Your email address was used to create a new account at {{config.site_name}} (https://{{settings.MAIN_DOMAIN}})." %} + {% email_body_content "To confirm your new account, go to this link:" %} +{% endblock body_content %} + +{% block body_button %} + {% email_button button_text="Confirm New Account" button_link="https://{{settings.MAIN_DOMAIN}}/auth/reset/{{reset.token}}/" %} +{% endblock body_button %} + +{% block body_signature %} + {% email_body_content "Thanks, Admins @{{settings.MAIN_DOMAIN}}" %} +{% endblock body_signature %} + +{% block extra_footer %} + {% email_footer "If this was not you, then please ignore this message - your email will not be used to make an account if this link is not visited." %} +{% endblock extra_footer %} +{% endautoescape %} diff --git a/templates/emails/base.html b/templates/emails/base.html new file mode 100644 index 0000000..f0e9d20 --- /dev/null +++ b/templates/emails/base.html @@ -0,0 +1,163 @@ +{% autoescape off %} + + + + + + {% block title %}{% endblock title %} + + + + + + + + + + + + + +{% endautoescape %} diff --git a/templates/emails/password_reset.html b/templates/emails/password_reset.html new file mode 100644 index 0000000..bb02565 --- /dev/null +++ b/templates/emails/password_reset.html @@ -0,0 +1,27 @@ +{% extends "emails/base.html" %} +{% load mail_tags %} +{% autoescape off %} +{% block title %}{{settings.MAIN_DOMAIN}} - Password Reset Requested{% endblock title %} +{% block subtitle %}{{settings.MAIN_DOMAIN}} - Password Reset Requested{% endblock subtitle %} + +{% block body_greeting %} + {% email_body_content "Hi there," %} +{% endblock body_greeting %} + +{% block body_content %} + {% email_body_content "A password reset was requested for your account ({{reset.user.email}}) at {{Config.system.site_name}} (https://{{settings.MAIN_DOMAIN}})." %} + {% email_body_content "To reset your password, go to this link:" %} +{% endblock body_content %} + +{% block body_button %} + {% email_button button_text="Reset Password" button_link="https://{{settings.MAIN_DOMAIN}}/auth/reset/{{reset.token}}/" %} +{% endblock body_button %} + +{% block body_signature %} + {% email_body_content "Thanks, Admins @{{settings.MAIN_DOMAIN}}" %} +{% endblock body_signature %} + +{% block extra_footer %} + {% email_footer "If this was not you, then please ignore this message - your password will not be reset if this link is not visited." %} +{% endblock extra_footer %} +{% endautoescape %} diff --git a/users/models/password_reset.py b/users/models/password_reset.py index c300d23..90c61fd 100644 --- a/users/models/password_reset.py +++ b/users/models/password_reset.py @@ -34,6 +34,14 @@ class PasswordResetStates(StateGraph): "settings": settings, }, ), + html_message=render_to_string( + "emails/account_new.html", + { + "reset": reset, + "config": Config.system, + "settings": settings, + }, + ), from_email=settings.SERVER_EMAIL, recipient_list=[reset.user.email], ) @@ -48,6 +56,14 @@ class PasswordResetStates(StateGraph): "settings": settings, }, ), + html_message=render_to_string( + "emails/password_reset.html", + { + "reset": reset, + "config": Config.system, + "settings": settings, + }, + ), from_email=settings.SERVER_EMAIL, recipient_list=[reset.user.email], )