0

Contact form data (using Wagtail's formbuilder) is not sending to email what is the issue?

I added email host also but not sending data to email

The form response (contact data) is being saved in wagtail admin but data not sending to email.

Console Output

DEBUG CONSOLE
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: contact form submission
From: archanapco8@gmail.com
To: ranjuranjitha. 1997@gmail.com
Date: Mon, 08 Nov 2021 08:13:45 -0000
Message-ID: <163635922547.9304.15785246130187863651@DESKTOP-NC1TOGR>
Auto-submitted: auto-generated
Your Name: ranjitha
Your company: mdrift
Your email address: rmanju@dgmail.com
When do you want to start?: 20-09-2021
What is your budget: 1000
Describe your needs, the more we know, the better: hi
[08/Nov/2021 13:43:45] "POST /contact-us/ HTTP/1.1" 200 9590
[08/Nov/2021 13:43:45] "GET /static/img/Thank-you.png HTTP/1.1" 200 15470
O

contact/models.py


from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField

class FormField(AbstractFormField):
    page = ParentalKey('Form Page', related_name='custom_form_fields')


class Form Page(AbstractEmailForm):
    thank_you_text = RichTextField(blank=True)

    content_panels = AbstractEmailForm.content_panels + [
        InlinePanel('custom_form_fields', label="Form fields"),
        FieldPanel('thank_you_text', classname="full"),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email Notification Config"),
    ]

    def get_form_fields(self):
        return self.custom_form_fields.all()

form_page.html


{% load static wagtailcore_tags widget_tweaks %}

{% block content %}

<h1>Contact</h1>
<br>

<form action="{% pageurl page %}" method="POST">
    {% csrf_token %}

    {% if form.non_field_errors %}
      <div class="alert alert-danger" role="alert">
        {% for error in form.non_field_errors %}
          {{ error }}
        {% endfor %}
      </div>
    {% endif %}

    {% for field in form.visible_fields %}
      <div class="form-group">
        {{ field.label_tag }}
        {% render_field field class+="form-control" %}
      </div>
    {% endfor %}

    <button type="submit" class="btn btn-primary" >Submit</button>
</form>

{% endblock %}

settings/base.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'ranjuranjitha.1997@gmail.com'
EMAIL_HOST_PASSWORD = 'Password$'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL ='ranjuranjitha.1997@gmail.com'
EMAIL_TO = 'ap8366106@gmail.com'

This is dev.py file. I given email backend also but not sending the mail.

settings/dev.py


from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-iu22rfee4za6ro+mez!4*@_trpy7!ebpbtu8iw$95v(rh_5fib'

# SECURITY WARNING: define the correct hosts in production!
ALLOWED_HOSTS = ['*'] 

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


try:
    from .local import *
except ImportError:
    pass
LB Ben Johnston
  • 4,751
  • 13
  • 29
  • Without having any error log output it is hard to tell but just checking you have ensured that your Gmail will allow SMTP sending? You need to ensure less secure apps are enabled - https://support.google.com/accounts/answer/6010255?hl=en - see also https://medium.com/@_christopher/how-to-send-emails-with-python-django-through-google-smtp-server-for-free-22ea6ea0fb8e However, using Gmail for this purpose is quite risky and likely to be blocked by Gmail and have spam issues. It is recommended you use a third party service, see https://pnote.eu/notes/django-app-engine-sending-email/ – LB Ben Johnston Nov 07 '21 at 03:12
  • less secure apps is enabled – Ranjitha Sanker Nov 08 '21 at 03:35
  • Ok. Please update the question with that fact and also with any logs you can find from the server output - otherwise it is not really possible to help without guessing. – LB Ben Johnston Nov 08 '21 at 07:11
  • [1]: https://i.stack.imgur.com/jXXq2.png This is the terminal which i got in my project...From and To mail id is there but it is not sending – Ranjitha Sanker Nov 08 '21 at 08:18
  • Ok. Can you please provide info about your settings/dev.py (dev settings) - most likely your EMAIL_BACKEND is set to console in develop mode which is why the email is outputting in the console and not sending via SMTP https://docs.djangoproject.com/en/3.2/topics/email/#console-backend – LB Ben Johnston Nov 08 '21 at 08:40
  • Ok. I added dev settings above. Please check – Ranjitha Sanker Nov 09 '21 at 06:16

1 Answers1

0

A few potential solutions below.

1. dev mode settings

Based on your log output and your settings/dev.py file, it appears that the email is working but is just logging the email.

In the dev settings you have EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend', this is a normal dev setup and leverages the Email Console backend which will never actually send an email but just log the email output to the console (terminal).

This means, while in development mode you can easily check the email content without actually sending a real email.

To temporarily override this you can create a file settings/local.py and put a different email backend in that file. EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

Then restart your dev server and it should use the override in the local.py file.

This works because of the settings/dev.py code you already have

try:
    from .local import *
except ImportError:
    pass

Note: Ensure that your settings/local.py is not committed to your code (git) as this may contain private secrets/passwords etc and usually is just a quick way to test something different.

Refer to this answer about managing Django settings for more insights. It would be good to read the full documentation page end to end that explains how sending email works in Django also.

2. Gmail settings

Check you have ensured that your Gmail will allow SMTP sending? You need to ensure less secure apps are enabled - support.google.com/accounts/answer/6010255?hl=en

See also How to send Django emails with SMTP for more details.

However, using Gmail for this purpose is quite risky and likely to be blocked by Gmail and have spam issues. It is recommended you use a third party service, see some recommendations for third party email services.

LB Ben Johnston
  • 4,751
  • 13
  • 29
  • I added everything but it shows gaierror at /contact-us/ this error how to solve this – Ranjitha Sanker Nov 10 '21 at 06:33
  • It is helpful to paste in the text (not an image) of the console error output in full, I am not familiar with `gaierror` (google says it is something about web sockets). The full error output is really important when trying to get help with errors you are encountering. – LB Ben Johnston Nov 10 '21 at 11:09
  • Also, I recommend you try to join the Slack group and get support there, this may be more suitable for specific problems encountered and saves you having to create a new question on SO (stack overflow) for something quite specific to your use case. https://docs.wagtail.io/en/stable/support.html#slack – LB Ben Johnston Nov 10 '21 at 11:11
  • This is not working in my project there is no errors but form is not sending to mail – Ranjitha Sanker Dec 17 '21 at 04:49
  • Hi. I cannot help further on this question, you will need to work out how to use print statements to debug the process and find out where the emails are being blocked. Or as noted above try to get help in a bit more of a chat environment. In general, without logs or details if is not possible for someone to help by just throwing out ideas of things to try. – LB Ben Johnston Dec 17 '21 at 08:31