Answer the question
In order to leave comments, you need to log in
How to send a contact form by email?
I made a contact form, everything seems to be working, I get the necessary form in the console, but it does not come to the mail. Tried with different mailboxes: Yandex, gmail.
For gmail in settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'test'
DEFAULT_FROM_EMAIL = 'test.ru'
DEFAULT_TO_EMAIL = '[email protected]'
def emailView(request):
if request.method == 'GET':
form = SignupForm()
else:
form = SignupForm(request.POST)
if form.is_valid():
email_subject = 'test.RU :: Запись на занятие '
email_body = "С сайта test.ru отправлено новое сообщение\n\n" \
"Имя отправителя: %s \n" \
"Фамилия отправителя: %s \n" \
"Контактный телефон: %s \n" \
"Фамилия и имя ребенка: %s \n" \
"Возраст ребенка: %s \n" \
"E-mail отправителя: %s \n\n" \
"Дополнительно: \n" \
"%s " % \
(form.cleaned_data['name'], form.cleaned_data['surname'], form.cleaned_data['tel'], form.cleaned_data['kid_name'], form.cleaned_data['kid_age'], form.cleaned_data['email'], form.cleaned_data['message'])
# Положим копию письма в базу данных
SignupModel.objects.create(name=form.cleaned_data['name'], surname=form.cleaned_data['surname'], tel=form.cleaned_data['tel'], kid_name=form.cleaned_data['kid_name'], kid_age=form.cleaned_data['kid_age'], email=form.cleaned_data['email'], message=form.cleaned_data['message'])
try:
send_mail(email_subject, email_body, settings.EMAIL_HOST_USER, ['[email protected]'], fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('signup_thx')
return render(request, 'signup/signup.html', {'form': form})
def successView(request):
return render(request, 'signup/signup_thx.html')
Answer the question
In order to leave comments, you need to log in
To send mail via smtp.gmail.com, you need credentials not from a regular mailbox, but from G Suite
. Perhaps it will be easier to use the smtp of your host or provider.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question