Opened 7 hours ago

Last modified 7 hours ago

#36110 new New feature

Unable to customize kwargs passed to PasswordResetForm EmailMultiAlternatives

Reported by: Claude Paroz Owned by:
Component: contrib.auth Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Use case: you'd like to add a Reply-To header to the password reset message.

To do that currently, you have no other choice than to rewrite the whole PasswordResetForm.send_mail method. I think a hook is needed to be able to define any other EmailMultiAlternatives init arguments.

Change History (1)

comment:1 by Claude Paroz, 7 hours ago

Note I didn't pass through a forum post as I think the need is rather straightforward. You may disagree :-)

A possible implementation:

diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index cd177fa5b6..56fedc4ae7 100644
--- a/django/contrib/auth/forms.py
+++ b/django/contrib/auth/forms.py
@@ -402,6 +402,14 @@ class PasswordResetForm(forms.Form):
         widget=forms.EmailInput(attrs={"autocomplete": "email"}),
     )
 
+    def extra_email_kwargs(self):
+        """
+        Allow setting extra EmailMultiAlternatives init parameters, after the
+        first four parameters (subject, body, from_email, to) which are defined
+        inside the send_mail() method.
+        """
+        return {}
+
     def send_mail(
         self,
         subject_template_name,
@@ -419,7 +427,7 @@ class PasswordResetForm(forms.Form):
         subject = "".join(subject.splitlines())
         body = loader.render_to_string(email_template_name, context)
 
-        email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
+        email_message = EmailMultiAlternatives(subject, body, from_email, [to_email], **self.extra_email_kwargs())
         if html_email_template_name is not None:
             html_email = loader.render_to_string(html_email_template_name, context)
             email_message.attach_alternative(html_email, "text/html")
Note: See TracTickets for help on using tickets.
Back to Top