From 9df3f4dd45c56938479cc383b4abbf54ee8aaadd Mon Sep 17 00:00:00 2001
From: Sebastian Haas
Date: Wed, 8 Nov 2023 23:40:24 +0100
Subject: [PATCH 1/2] Retrieve email subject from template
Instead of a direct setting, the email subject can now be set using a template.
This is heavily inspired and pretty much copied from the way Django is rendering the email subject for its password reset links.
See https://github.com/django/django/blob/main/django/contrib/auth/templates/registration/password_reset_subject.txt
See https://docs.djangoproject.com/en/4.2/topics/auth/default/#django.contrib.auth.views.PasswordResetView.subject_template_name
---
verify_email/app_configurations.py | 6 ++---
verify_email/email_handler.py | 16 +++++++++----
verify_email/locale/de/LC_MESSAGES/django.po | 24 +++++++++++++++++++
.../email_verification_subject.txt | 4 ++++
4 files changed, 43 insertions(+), 7 deletions(-)
create mode 100644 verify_email/locale/de/LC_MESSAGES/django.po
create mode 100644 verify_email/templates/verify_email/email_verification_subject.txt
diff --git a/verify_email/app_configurations.py b/verify_email/app_configurations.py
index 5efa7b5..b35338d 100644
--- a/verify_email/app_configurations.py
+++ b/verify_email/app_configurations.py
@@ -25,9 +25,9 @@ def __init__(self):
False
),
- 'subject': (
- "SUBJECT",
- "Email Verification Mail"
+ 'email_verification_subject_template': (
+ "EMAIL_VERIFICATION_SUBJECT_TEMPLATE",
+ "verify_email/email_verification_subject.txt"
),
'email_field_name': (
diff --git a/verify_email/email_handler.py b/verify_email/email_handler.py
index b3fae6f..32cac51 100644
--- a/verify_email/email_handler.py
+++ b/verify_email/email_handler.py
@@ -19,14 +19,19 @@ def __init__(self):
self.token_manager = TokenManager()
# Private :
- def __send_email(self, msg, useremail):
- subject = self.settings.get('subject')
+ def __send_email(self, subject, msg, useremail):
send_mail(
subject, strip_tags(msg),
from_email=self.settings.get('from_alias'),
recipient_list=[useremail], html_message=msg
)
+ def __get_subject(self, request):
+ subject_template_name = self.settings.get('email_verification_subject_template')
+ subject = render_to_string(subject_template_name, dict(), request=request)
+ # Email subject *must not* contain newlines
+ return "".join(subject.splitlines())
+
# Public :
def send_verification_link(self, request, inactive_user=None, form=None):
@@ -52,8 +57,9 @@ def send_verification_link(self, request, inactive_user=None, form=None):
{"link": verification_url, "inactive_user": inactive_user},
request=request
)
+ subject = self.__get_subject(request)
- self.__send_email(msg, useremail)
+ self.__send_email(subject, msg, useremail)
return inactive_user
except Exception:
inactive_user.delete()
@@ -88,7 +94,9 @@ def resend_verification_link(self, request, email, **kwargs):
self.settings.get('html_message_template', raise_exception=True),
{"link": link}, request=request
)
- self.__send_email(msg, email)
+ subject = self.__get_subject(request)
+
+ self.__send_email(subject, msg, email)
return True
diff --git a/verify_email/locale/de/LC_MESSAGES/django.po b/verify_email/locale/de/LC_MESSAGES/django.po
new file mode 100644
index 0000000..ea97800
--- /dev/null
+++ b/verify_email/locale/de/LC_MESSAGES/django.po
@@ -0,0 +1,24 @@
+# Django-Verify-Email
+# Copyright (C) 2023 Django-Verify-Email contributors
+# This file is distributed under the same license as the PACKAGE package.
+# Sebastian Haas, 2023
+# SECOND AUTHOR , YEAR.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: \n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2023-11-06 14:25+0100\n"
+"PO-Revision-Date: 2023-11-06 14:27+0100\n"
+"Last-Translator: \n"
+"Language-Team: \n"
+"Language: de\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+"X-Generator: Poedit 3.4.1\n"
+
+#: templates/verify_email/email_verification_subject.txt:3
+msgid "Confirm Your Email Address"
+msgstr "E-Mail-Adresse bestätigen"
diff --git a/verify_email/templates/verify_email/email_verification_subject.txt b/verify_email/templates/verify_email/email_verification_subject.txt
new file mode 100644
index 0000000..6384541
--- /dev/null
+++ b/verify_email/templates/verify_email/email_verification_subject.txt
@@ -0,0 +1,4 @@
+{% load i18n %}
+{% autoescape off %}
+{% blocktranslate %}Confirm Your Email Address{% endblocktranslate %}
+{% endautoescape %}
From 39cf87a238e12c3eedd91cdb4c213116df2d115f Mon Sep 17 00:00:00 2001
From: Sebastian Haas
Date: Wed, 8 Nov 2023 23:40:39 +0100
Subject: [PATCH 2/2] Update README.md to reflect changed subject configuration
---
README.md | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index 00ee391..e500399 100644
--- a/README.md
+++ b/README.md
@@ -289,6 +289,8 @@ The app is packed with default HTML templates to handle the web pages but if you
```
HTML_MESSAGE_TEMPLATE = "path/to/html_template.html"
+EMAIL_VERIFICATION_SUBJECT_TEMPLATE = "path/to/subject.txt"
+
VERIFICATION_SUCCESS_TEMPLATE = "path/to/success.html"
VERIFICATION_FAILED_TEMPLATE = "path/to/failed.html"
@@ -299,11 +301,6 @@ LINK_EXPIRED_TEMPLATE = 'path/to/expired.html'
NEW_EMAIL_SENT_TEMPLATE = 'path/to/new_email_sent.html'
```
-```
-SUBJECT = 'subject of email'
-
-# default subject is: Email Verification Mail
-```
## Inside Templates :