Benjamin Carafa
Benjamin Carafa

Reputation: 633

Django contact form: email not sending

I'm currently building a contact form for my website in Django. The only problem is that the email is not being sent when the form is submitted. This is the code:

settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "c******[email protected]"
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = '587'

views.py:

sender_name = form.cleaned_data['nome_completo']
            sender_email = form.cleaned_data['email']
            message = "{0} has sent you a new message:\n\n{1}".format(sender_name, form.cleaned_data['messaggio'])

            send_mail('New Enquiry', message, sender_email, ['c******[email protected]'])

My thought: Since I'm in a virtualenv, when I submit the form, the terminal displays that it was successfully submitted, and gets me back the code right, maybe the email is not sent because of that, or maybe because I'm using a gmail account? Thanks!

Upvotes: 0

Views: 129

Answers (2)

talkingtoaj
talkingtoaj

Reputation: 888

So the issue is with EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

This outputs emails to your console for debugging instead of actually sending emails. (See https://docs.djangoproject.com/en/3.1/topics/email/#console-backend)

Don't set EMAIL_BACKEND to anything for production as it defaults to django.core.mail.backends.smtp.EmailBackend. Then your EMAIL_HOST settings will take effect and the email should be sent out.

Upvotes: 1

Redgren Grumbholdt
Redgren Grumbholdt

Reputation: 1230

Since you are using an SMTP server, why not use the following backend:

django.core.mail.backends.smtp.EmailBackend

Also, if you are using Gmail, make sure of the following:

  1. Two-Factor authentication is enabled on account
  2. You are using an app password.

You can find these on the security tab on your google account page:

https://myaccount.google.com/security

Upvotes: 1

Related Questions