Opened 5 weeks ago

Closed 5 weeks ago

#36110 closed New feature (wontfix)

Unable to customize kwargs passed to PasswordResetForm EmailMultiAlternatives

Reported by: Claude Paroz Owned by: Antoliny
Component: contrib.auth Version: dev
Severity: Normal Keywords: email
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 (6)

comment:1 by Claude Paroz, 5 weeks 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")

comment:2 by Antoliny, 5 weeks ago

Keywords: email added
Owner: set to Antoliny
Status: newassigned

May I work on this issue? If you decide to take it on, feel free to unassign me. 😃

Last edited 5 weeks ago by Antoliny (previous) (diff)

comment:3 by Claude Paroz, 5 weeks ago

Sure, but please wait for the ticket approval first.

comment:4 by Tim Graham, 5 weeks ago

The use case was mentioned in #17431 where PasswordResetForm.send_mail() was added. I agree it's not so nice to have to override the entire method, however, the test added in a00b78b1e2ac9bf271d55c1799138a27f5e0d03e adds Reply-To and does not look so ugly.

Also note that much this form's API is controlled in the view via form.save() kwargs. In light of that, it looks a bit out of place to add a method hook for the proposed purpose.

comment:5 by Claude Paroz, 5 weeks ago

and does not look so ugly

However it hardcodes things to not look ugly!

it looks a bit out of place to add a method hook for the proposed purpose

Sure, the implementation could be different than the one I proposed. I'm not opposed to make the customization as a new form.save() kwarg.

comment:6 by Sarah Boyce, 5 weeks ago

Resolution: wontfix
Status: assignedclosed

However it hardcodes things to not look ugly!

True, though I believe if you needed to overwrite send_mail, it's probable that you can simplify and hard-code some logic (I might be wrong)

I am on the fence here, so would prefer for this to be discussed on the forum and see if there are other folks who are in favor of the change

Note: See TracTickets for help on using tickets.
Back to Top