P
P
PiggyPig2017-11-07 13:56:58
Django
PiggyPig, 2017-11-07 13:56:58

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'

views.py
# ...
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})

In general, I have no idea how to make this mail work. The protocol at the moment is http, but I think this is not the reason (although you are already thinking of everything out of desperation).
The form itself is working, earlier instead of the send_mail() function I used print() to the console - everything worked.
It seems like the code is no different from the examples on different sites, but it still doesn’t work. Help me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PiggyPig, 2017-11-11
@PiggyPig

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)

B
Bulbashful, 2017-11-09
@Bulbashful

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 question

Ask a Question

731 491 924 answers to any question