Scenario:
In my django application, admin should create users using 'username' and 'email', 'password' will be generated auto .Welcome email will be sent to users with redirect link to reset password.
Issue:
after entering email for reset password the app should send email with link to confirm reset password. THE PROBLEM is the email it never show in user "inbox" but i can find it in app "outbox mails"
How can i make it appear in user mailbox?
Any Help or idea?
I'm using:
-Python3
-Django4
-enable two factor authentication(2FA) for Google account
Settings.py:
#SMTP Config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "webapp#app.com"
DEFAULT_FROM_EMAIL = "MyApp <no-reply#app.com>"
EMAIL_HOST_PASSWORD = "******"
APP/URLS:
urlpatterns = [
path('',views.login_user, name ='login'), #login fonction path
path ('user_logout',views.user_logout, name = 'logout'), #logout function path
path('dashboard/', views.dashboard, name='dashboard'),# dashoard view path
path ('daily',views.DailyView, name = 'dailydashboard'),#daily view depath
path ('monthly',views.MonthlyView, name = 'Monthlydashboard'),#monthly view path
path ('yearly',views.YearlyView, name = 'Yearlydashboard'), #yearly view path
path("reset_password/", auth_views.PasswordResetView.as_view(template_name="password/password_reset.html"), name="reset_password"),
path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(template_name="password/password_reset_done.html"), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="password/password_reset_confirm.html"), name='password_reset_confirm'),
path('password_reset_complete/', auth_views.PasswordResetCompleteView.as_view(template_name='password/password_reset_complete.html'), name='password_reset_complete'),
]
Related
I`m using the Django authentication system to reset the user password. I create an app named account, in which I add the following codes in the urls.py modules:
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
urlpatterns=[
path('password_reset/',
auth_views.PasswordResetView.as_view(),
name='password_reset'),
path('password_reset/done/',
auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
path('password_reset_confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(
template_name='registration/password_reset_confirm.html'),
name='password_reset_confirm'),
path('reset/done/',
auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete'),
]
I create all the templated needed in the app`s template/registration repository:
password_reset_form.html, password_reset_done.html, password_reset_confirm.html, password_reset_complete.html,
And I also add the email setting to my project:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '********'
EMAIL_HOST_PASSWORD = '**********'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
I run the server, on the password reset page I enter the email that the user is verified, after that, I received the password reset link in my inbox, but When I click it then the TemplateDoesNotExist exception occurs.
I read the Django Documentation of Using the Django authentication system at https://docs.djangoproject.com/en/3.1/topics/auth/default/#built-in-auth-views.
Then I change the template name password_reset_confirm.html to password-reset-confirm.html, and somehow it works!
Is 'registration' the name of the folder where you put all of your templates files? if it is, all of the as_view() should have template_name inside them e.g auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'), name='password_reset')
I made a Django application with custom users.
I followed the docs to create my password reset pages and forms. I found this similar question and tweaked my configuration to use the right domain name. My SITE_ID is set to 1, and I'm sure the Site with pk=1 is configured to be my domain.
When I try to get a password reset email, the subject contains the correct URL for the site I am requesting the password reset for. (Subject is "Password reset for myMainDomain.com")
The problem is the link inside the mail: the auto-generated link points to a CNAME of my mailserver, for some unknown reason. Django shouldn't even know that, since the only place I put the mailserver's domain is in the mail settings in setting.py
My users.urls.py:
urlpatterns = [path('accounts/', include('django.contrib.auth.urls')),
path('', views.UserListView.as_view(), name='lista-user'),
path('accounts/signup/', views.Iscriviti.as_view(), name='signup'),
path('<int:pk>', views.Profilo.as_view(), name='profilo'),
path('accounts/edit/<int:pk>', views.Modifica.as_view(), name='modifica-utente')]
My template password_reset_email.html:
Here is your password-reset link:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
Relevant part of my settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.myMailServerdomain.it'
EMAIL_PORT = '25'
EMAIL_HOST_USER = 'MYUSER'
EMAIL_HOST_PASSWORD = 'MYPASS'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = False
DEFAULT_FROM_EMAIL = 'noreply#myMailServerdomain.it'
SITE_ID = 1 # -> This is the site I configured on Django, with my_site.domain = 'myMainDomain.com' and my_site.name = 'My site's name'
The email I get when I try a password reset:
Here is your password-reset link:
http://tracking.mymailserverdomain.it/f3ag34/Go6CyRfZ=0D
tracking.mymailserverdomain.it is a CNAME of my domain mymailserverdomain.it which I pointed to track.mail.mymailserverdomain.it. I tried to send an empty mail with {{domain}} inside to see if my SMTP server used that for some reason, but it doesn't and just puts {{domain}} in the mail.
How can I investigate this behavior?
I created the Django email backend for active the email during the registration. But in my case When I register the account and confirmation email is sent on my email and after click on the confirmation link then it's not activate the user's account and link is not redirected on login page.
setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your_mail#gmail.com'
EMAIL_HOST_PASSWORD = 'XXXXXXXXXX'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = "USERNAME"
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = reverse_lazy('account_confirm_complete')
ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = reverse_lazy('account_confirm_complete')
urls.py
urlpatterns = [
url(r'^user_reg/registration/account-email-verification-sent/', email_view.null_view, name='account_email_verification_sent'),
url(r'^user_reg/registration/account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(), name='account_confirm_email'),
url(r'^user_reg/registration/complete/$', email_view.complete_view, name='account_confirm_complete'),
url(r'^user_reg/password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', email_view.null_view, name='password_reset_confirm'),
path('admin/', admin.site.urls),
path('user_reg/', include('users.urls', namespace="users")),
path('user_reg/registration/', include('rest_auth.registration.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And below the email message
Greeting from example.com!
You're receiving this e-mail because user usertest has given yours as an e-mail address to connect their account via API.
To confirm this is correct, go to http://127.0.0.1:8000/user_reg/registration/account-confirm-email/Mjg:1jSeJC:4btJkSnHSxYN7w5CITEPydcG9cA/
Thank you from example.com!
example.com
Where is the problem and how can I solve this?.
Please help me. Thank you!
I am trying to send password change link to an email address which a user will type. I typed my email but it is not sending me any link. How to resolve this issue?
urls
urlpatterns = [
path('password_reset/',auth_views.PasswordResetView.as_view
(template_name='users/password_reset.html'),
name='password_reset'),
path('password_reset_done/',auth_views.PasswordResetDoneView.as_view
(template_name='users/password_reset_done.html'),
name='password_reset_done'),
path('password_reset_confirm/',auth_views.PasswordResetConfirmView.as_view
(template_name='users/password_reset_confirm.html'),
name='password_reset_confirm')]
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')
I use these as my urls. The main difference I spot with yours right away is with password-reset-confirm. Make sure your passing the token.
from django.contrib.auth import views as auth_views
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'), name='password_reset'),
path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'), name='password_reset_confirm'),
path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'),
path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'),
My settings look like this
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_POST = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('traces_email')
EMAIL_HOST_PASSWORD = os.environ.get('traces_email_password')
Also please note that you need to set up a g-mail account to allow Django or any other app to access it, it doesn't work automatically. The password you receive after doing this IS NOT the same as the password you normally log in with. It may be this that is causing you issues.
As I saw you are missing the tokens in your password-reset-confirm URL it is perhaps also the problem that your don't have a token generator.
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
)
account_activation_token = TokenGenerator()
I created this in a file named token_generator.py.
In Django my project I add the "Frorget password" link.
In my urls.py:
path('accounts/', include('django.contrib.auth.urls')),
in my settings.py:
# Email setting
EMAIL_USE_TLS = True
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com'
EMAIL_PORT = 587
EMAIL_FROM = 'account#test.io'
EMAIL_HOST_USER = 'YUIJKHKBKBD7879'
EMAIL_HOST_PASSWORD = 'By6786ghgkgk//89jkjlnCkVt'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
but when I go to my page /accounts/password_reset/ and insert the email for what I whant to change the password es. mario.rossi#gmail.com, when I click The Submit button, system return:
SMTPDataError at /accounts/password_reset/
(554, b'Message rejected: Email address is not verified. The following identities failed the check in region US-EAST-1: mario.rossi#gmail.com, webmaster#localhost')
But mario.rossi#gmail.com is not the sender, it's the receiver! Why django reset password does seem to use EMAIL_FROM from settings.py for send email From?