A
A
azhlbn2018-11-25 16:48:13
Django
azhlbn, 2018-11-25 16:48:13

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]'

Tried DisplayUnlockCaptcha for gmail - didn't help. Maybe you need to somehow configure your mailbox?
views.py
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

2 answer(s)
S
Stalker_RED, 2018-11-25
@azhlbn

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.

D
Dimonchik, 2018-11-25
@dimonchik2013

Yandex

login = 'login_without_DOG'
password = 'password_as_is'
host = 'smtp.yandex.com'
port = 465

gmail (without GSuite)
login = '[email protected]'
password = 'password_for_application_if_two_faktor_enbaled'
host = 'smtp.gmail.com'
port = 587

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question