Django password reset. Not sending mail

非 Y 不嫁゛ 提交于 2020-12-04 19:34:14

问题


I'm trying to get the django password reset working but the reset email does not get sent.

I know my email is properly configured because the following works both in the shell and in one of my views (I'm using it to get support email to myself).

from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.',
          'admin@mydomain.com',['me@gmail.com'], fail_silently=False)

I can get to my reset password view (password/reset/) and after I give it my email it correctly redirects me to password/reset/done/ but it doesn't sends the email.

Here's my urls.py:

(r'^password/reset/$','django.contrib.auth.views.password_reset'),
(r'^password/reset/done/$','django.contrib.auth.views.password_reset_done'),
(r'^password/reset/confirm/$','django.contrib.auth.views.password_reset_confirm'),
(r'^password/reset/complete/$','django.contrib.auth.views.password_reset_confirm'),
(r'^password/change/$','django.contrib.auth.views.password_change'),
(r'^password/change/done/$','django.contrib.auth.views.password_change_done'),

Here's my password_reset_form.html:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="/media/css/style_login.css" />
    <title>Información de acceso requerida</title>
</head>
<body>
    <div id="wrapper">
        <h1>Recuperar password</h1>
        <p>Utilice este formulario cuando desee recuperar el password para su usuario.</p>
        {% if form.errors %}
        <p>No hay un usuario registrado con ese correo electronico.</p>
        {% endif %}
        <form method="post" action="{% url django.contrib.auth.views.password_reset_done %}">
            {% csrf_token %}
            {{ form }}
            <input class="login" type="submit" value="Recuperar" />
        </form>
    </div>
</body>

Any ideas? Thanks


回答1:


I should have mention I'm using hostgator as my provider. So this is my settings.py

EMAIL_HOST      = 'my-domain.com'
EMAIL_HOST_PASSWORD = 'my cpanel password'
EMAIL_HOST_USER = 'my cpanel user'
EMAIL_PORT      = 25
EMAIL_USE_TLS   = False
DEFAULT_FROM_EMAIL  = 'webmaster@my-host.com'
SERVER_EMAIL    = 'root@my-domain.com'

The above settings work!




回答2:


In my case I created the users using create_user(). My idea was to create a bunch of accounts and then tell people they could go to the password reset form to set their password. I think it sucks if you need to tell them 'Use password "welcome123" and then don't forget to modify your password' or such.

I found out if I did not pass Password='foo' or also if I passed Password=None to create_user() password resets are not sent. This is because Django sometimes uses an empty password to know that it does not need to authenticate locally, but rather use LDAP or such.

So now I do this:

User.objects.create_user(
  user_name,
  email=user_email,
  password=os.urandom(32),
)

And I can direct people to use the password reset function for their new accounts. I think it would be awesome if I had an option in create_user to automatically send emails for account activation to users, though!




回答3:


You need to add a default from address e.g:

DEFAULT_FROM_EMAIL = 'info@mymail.com'

I see now it is in the comments but will add it here in case anyone misses them too.



来源:https://stackoverflow.com/questions/8609946/django-password-reset-not-sending-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!