error on sending mail while test sending mail successfully in Django - django

I'm coding about Forget Password feature on Django. Reset mail will be sent when user fill out form and click Reset button. But I have a trouble here, mail isn't sent after user click button. I use sendtestmail command, sending mail successfully. And more, the content and mail subject that I define is not in the email it sent when I check by sendtestmail command. It took me all yesterday to fix above error but didn't work. Can somebody help me.
settings.py
...
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', 'vuthehuy.hus#gmail.com')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', 'my_password_app')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL', 'Colo Shop <no-reply#localhost>')
app.urls
urlpatterns = [
path('password_reset/', auth_view.PasswordResetView.as_view(
template_name='account/password_reset_form.html',
form_class=EmailForgotPassword,
email_template_name='account/password_reset_email.html',
subject_template_name='account/password_reset_subject.txt'
), name='password_reset'),
path('password_reset/done/', auth_view.PasswordResetDoneView.as_view(
template_name='account/password_reset_done.html'
), name='password_reset_done'),
path('password_reset/confirm/<uidb64>/<token>/', auth_view.PasswordResetConfirmView.as_view(
template_name='account/password_change.html'
), name='password_reset_confirm'),
path('password_reset/complete/', auth_view.PasswordResetCompleteView.as_view(
template_name='account/password_change_done.html'
), name='password_reset_complete'),
]

Related

how do I configure my Django app to use Dreamhost email?

I created a django app that has a contact form where users can send an email to the company (The email configured in the APP)
I am getting "SERVER ERROR 500"
After reading this:
django email settings on dreamhost
I tried the following format in my settings.py:
EMAIL_HOST = 'smtp.dreamhost.com' # I also tried pop.dreamhost.com / imap.dreamhost.com with their respective port numbers.
EMAIL_USE_TLS = True # I also tried EMAIL_USE_SSL
EMAIL_PORT = 587 # I also tried 25 and 465
EMAIL_HOST_PASSWORD = 'EMAIL_PASSWORD' # also tried SERVER_PASSWORD
EMAIL_HOST_USER = 'my_dreamhost_email' # Also tried admin#mydomain.com
EMAIL_SUBJECT_PREFIX = ''
SERVER_EMAIL = 'my_dreamhost_email' # also tried 'localhost' / 'admin#mydomain.com'
ADMINS = (('Jack Shedd', 'jack#example.com'),) # I used my details
PS: The app is working perfectly if I use gmail
What are the correct details to use?
Some thoughts about that:
Change your settings.py:
DEBUG = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ADMIN_EMAIL = 'Dont reply <no-reply#your_dreamhost_email.com>'
SUPPORT_EMAIL = 'Dont reply <no-reply#your_dreamhost_email.com>'
DEFAULT_FROM_EMAIL = ADMIN_EMAIL
SERVER_EMAIL = ADMIN_EMAIL

How can I get Django to send error emails on status codes above 500?

I have migrated an old codebase to a new server and am now running Django 4.0.5.
I can send emails from the shell as follows:
from django.core.mail import mail_admins
mail_admins(subject='test', message='test')
But I don't receive any emails on 500 or higher errors.
My settings are:
DEBUG = env('DEBUG')
ADMINS = [('My Name', 'me#provider.com'),]
MANAGERS = ADMINS
ADMIN_EMAIL = ['me#provider.com',]
DEFAULT_FROM_EMAIL = 'admin#provider.com'
SERVER_EMAIL = 'admin#provider.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'admin#provider.com'
EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
And i have tried adding:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
But they are still not sent.
I am using django-environ, but debug is set to false.
Any ideas what the problem could be?
Have you checked this?
may be the problem is this.
https://support.google.com/accounts/answer/6010255?hl=en#:~:text=To%20help%20keep,continue%20to%20read.

How can I send a reset password email on Django?

During the process of creating my first website using Django framework, I encountered a little problem that I couldn't found a solution yet. So, when an user wants to reset his or her password, i'd like to send to him/her a reset mail. So far, I have this:
urls.py
from django.contrib.auth import views as auth_views
......
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'),
name='password_reset'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='registration/password_reset_confirm.html'),
name='password_reset_confirm'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='registration/password_reset_done.html'),
name='password_reset_done'),
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='registration/password_reset_complete.html')),
....
settings.py
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
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')
I created a token generator for my link:
token_generator.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
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()
When I go through the reset flow, it does not send any email. It is still sent to my terminal.
Can somebody help me with this issue? Thank you so much for your time!
This setting
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
tells django to send the message to your terminal. To actually send an email, you need to use
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
This is described in the Django Docs.

Authentication Required error when sending email with Django

I'm trying to send a password-reset email using gmail server but I'am getting an error. 'SMTPSenderRefused Authentication Required'
The google account have - 1. 2-Step Verification on 2. Third-party access through app password 3. Access allowed for less secure app : On
'''
setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER') #my gmail acc
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD') #app password from gmail acc
EMAIL_PORT = 587
'''
'''
urls.py
path('password-reset/', PasswordResetView.as_view(
template_name='users/password-reset.html'), name='password_reset'),
path('password-reset-done/', PasswordResetDoneView.as_view(
template_name='users/password-reset-done.html'),
name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
PasswordResetConfirmView.as_view(
template_name='users/password_reset_confirm.html'),
name='password_reset_confirm'),
'''
I expect an email with a reset-password-link send to the user or whomever POSTed their email when prompted but what i get is this error -
'''
SMTPSenderRefused at /password-reset/
(530, b'5.5.1 Authentication Required. Learn more at\n5.5.1
https://support.google.com/mail/?p=WantAuthError p17sm3671371wrq.95 -
gsmtp', 'webmaster#localhost')
'''
create a new account in google and avoid two step verification.
Add your account name and password to settings directly
It will work
As you are new, hardcore these details directly to settings.py later you can configure in local environment

MailgunAPIError<Response [400]> with django-registration-redux

I'm trying to send registration emails by django-registration-redux,
but got this response code: MailgunAPIError Response [400].
The code means 'Bad Request - Often missing a required parameter'
But i can send an email manually on my site, it's functional.
Am I missed some required parameter between registration-redux and mailgun?
This is my setting:
INSTALLED_APPS = (...,
'registration' ,
)
ACCOUNT_ACTIVATION_DAYS = 7
EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = 'xxxx'
MAILGUN_SERVER_NAME = 'xxxx'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_HOST_USER='postmaster#xxxx'
EMAIL_HOST_PASSWORD= 'xxxx'
EMAIL_PORT = 587
And this is my urls setting
path('accounts/', include('registration.backends.default.urls')
I solved the problem by myself.
I changed the EMAIL_BACKEND from 'django_mailgun.MailgunBackend' to 'django.core.mail.backends.smtp.EmailBackend' and it's OK.