Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Permit custom from-email address in auth forms email.

Patch from cassidy and Rob Hudson. Fixed #11300.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13817 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4084bc735437e7a2e2a09cff469788e036080203 1 parent 631bb7d
Malcolm Tredinnick authored
4  django/contrib/auth/forms.py
@@ -117,7 +117,7 @@ def clean_email(self):
117 117
         return email
118 118
 
119 119
     def save(self, domain_override=None, email_template_name='registration/password_reset_email.html',
120  
-             use_https=False, token_generator=default_token_generator):
  120
+             use_https=False, token_generator=default_token_generator, from_email=None):
121 121
         """
122 122
         Generates a one-use only link for resetting password and sends to the user
123 123
         """
@@ -140,7 +140,7 @@ def save(self, domain_override=None, email_template_name='registration/password_
140 140
                 'protocol': use_https and 'https' or 'http',
141 141
             }
142 142
             send_mail(_("Password reset on %s") % site_name,
143  
-                t.render(Context(c)), None, [user.email])
  143
+                t.render(Context(c)), from_email, [user.email])
144 144
 
145 145
 class SetPasswordForm(forms.Form):
146 146
     """
1  django/contrib/auth/tests/urls.py
@@ -16,6 +16,7 @@ def remote_user_auth_view(request):
16 16
     (r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
17 17
     (r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')),
18 18
     (r'^remote_user/$', remote_user_auth_view),
  19
+    (r'^password_reset_from_email/$', 'django.contrib.auth.views.password_reset', dict(from_email='staffmember@example.com')),
19 20
     (r'^login_required/$', login_required(password_reset)),
20 21
     (r'^login_required_login_url/$', login_required(password_reset, login_url='/somewhere/')),
21 22
 )
8  django/contrib/auth/tests/views.py
@@ -62,6 +62,14 @@ def test_email_found(self):
62 62
         self.assertEquals(response.status_code, 302)
63 63
         self.assertEquals(len(mail.outbox), 1)
64 64
         self.assert_("http://" in mail.outbox[0].body)
  65
+        self.assertEquals(settings.DEFAULT_FROM_EMAIL, mail.outbox[0].from_email)
  66
+
  67
+    def test_email_found_custom_from(self):
  68
+        "Email is sent if a valid email address is provided for password reset when a custom from_email is provided."
  69
+        response = self.client.post('/password_reset_from_email/', {'email': 'staffmember@example.com'})
  70
+        self.assertEquals(response.status_code, 302)
  71
+        self.assertEquals(len(mail.outbox), 1)
  72
+        self.assertEquals("staffmember@example.com", mail.outbox[0].from_email)
65 73
 
66 74
     def _test_confirm_start(self):
67 75
         # Start by creating the email
3  django/contrib/auth/views.py
@@ -105,7 +105,7 @@ def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_N
105 105
 def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
106 106
         email_template_name='registration/password_reset_email.html',
107 107
         password_reset_form=PasswordResetForm, token_generator=default_token_generator,
108  
-        post_reset_redirect=None):
  108
+        post_reset_redirect=None, from_email=None):
109 109
     if post_reset_redirect is None:
110 110
         post_reset_redirect = reverse('django.contrib.auth.views.password_reset_done')
111 111
     if request.method == "POST":
@@ -114,6 +114,7 @@ def password_reset(request, is_admin_site=False, template_name='registration/pas
114 114
             opts = {}
115 115
             opts['use_https'] = request.is_secure()
116 116
             opts['token_generator'] = token_generator
  117
+            opts['from_email'] = from_email
117 118
             if is_admin_site:
118 119
                 opts['domain_override'] = request.META['HTTP_HOST']
119 120
             else:
7  docs/topics/auth.txt
@@ -906,7 +906,7 @@ includes a few other useful built-in views located in
906 906
           default to :file:`registration/password_change_done.html` if not
907 907
           supplied.
908 908
 
909  
-.. function:: views.password_reset(request[, is_admin_site, template_name, email_template_name, password_reset_form, token_generator, post_reset_redirect])
  909
+.. function:: views.password_reset(request[, is_admin_site, template_name, email_template_name, password_reset_form, token_generator, post_reset_redirect, from_email])
910 910
 
911 911
     Allows a user to reset their password by generating a one-time use link
912 912
     that can be used to reset the password, and sending that link to the
@@ -932,6 +932,11 @@ includes a few other useful built-in views located in
932 932
         * ``post_reset_redirect``: The URL to redirect to after a successful
933 933
           password change.
934 934
 
  935
+        .. versionchanged:: 1.3
  936
+
  937
+        * ``from_email``: A valid e-mail address. By default Django uses
  938
+          the :setting:`DEFAULT_FROM_EMAIL`.
  939
+
935 940
     **Template context:**
936 941
 
937 942
         * ``form``: The form for resetting the user's password.

0 notes on commit 4084bc7

Please sign in to comment.
Something went wrong with that request. Please try again.