Answer the question
In order to leave comments, you need to log in
Why is the form not being sent to Gmail mail?
Similar questions have been asked before, but either the answers did not solve the problem, or the question was never answered.
Actually my problem is the following error when submitting the form:
"... Username and Password not accepted. ...."
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'password'
# ...
from django.core.mail import send_mail
from django.conf import settings
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
try:
send_mail(subject, message, EMAIL_HOST_USER, [EMAIL_HOST_USER], fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid header found')
return HttpResponse('Сообщение отправлено')
else:
form = ContactForm()
return render(request, 'app/page.html', {'form': form})
Answer the question
In order to leave comments, you need to log in
The issue was resolved after completing the following steps:
1) Switching to the https protocol
2) Replacing the EMAIL_HOST_USER variable in all places in the view with a string value
3) Replacing the mail password with the application password in settings.py (if two-step authentication is enabled)
Did you write the settings for EMAIL_HOST_USER/PASSWORD for the demo? They contain a specific email and password.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question