简体   繁体   中英

Sending mail though django

In my project, I tried to allow users to change and reset password. But can't figure out why this mail configuration isn't working. Actually, it doesn't send mail to the user. But showing e-mail has been sent on API.

Here is my code:

settings.py

REST_SESSION_LOGIN = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_VERIFICATION = 'optional'

#mail config

EMAIL_HOST = 'smtp.gmail.net'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'mygmail@gmail.com'
EMAIL_HOST_PASSWORD = '************'    #gmail app password
EMAIL_USE_TLS = True

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('authentication/', include('dj_rest_auth.urls')),
    path('authentication/registration/', include('dj_rest_auth.registration.urls')),
    path('api/', include('articles.api.urls')),
    re_path(r'^authentication/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm')
]

And in terminal at from it shows webmaster@localhost . But why it isn't showing EMAIL_HOST_USER e-mail.

Subject: Password reset on example.com
From: webmaster@localhost
To: gicof53256@tdcryo.com
Date: Sun, 29 Nov 2020 08:14:16

When you have mentioned EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' this will mock the email, but not actually send it. To actually send it, replace it with the below settings.

Also, it's better to store the credentials in environment variables. I am attaching the code snippet that will do the job.

EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM