Contact Form Processing in Django, Email Not Submitted

I have an application in which the user can contact me by filling out a form and in a form that the user simply needs to fill in with his details, email and subject.

The code does not cause an error, but I could not receive mail after setting everything up, but the contact data will be saved in the database, since I want it to be saved.

Below is my code.

Models.py

class Contact(models.Model): name = models.CharField(max_length=100) message = models.TextField() sender = models.EmailField() phone = models.CharField(max_length=10) cc_myself = models.BooleanField(blank=True) time = models.DateTimeField(auto_now_add=True, db_index=True) def __str__(self): return 'Message for {}'.format(self.sender) 

Forms.py

 class ContactForm(forms.ModelForm): class Meta: model = Contact fields = ['name', 'sender', 'phone', 'message', 'cc_myself'] 

Views.py

 def contact(request): if request.method == 'POST': contact_form = ContactForm(request.POST) if contact_form.is_valid(): name = contact_form.cleaned_data['name'] message = contact_form.cleaned_data['message'] sender = contact_form.cleaned_data['sender'] phone = contact_form.cleaned_data['phone'] cc_myself = contact_form.cleaned_data['cc_myself'] recipients = [' xxxx@gmail.com '] if cc_myself: recipients.append(sender) send_mail(name, message, sender, recipients) contact_form.save() messages.success(request, 'Message sent successfully') return redirect('contact:contact') else: messages.error(request, 'Error sending your Message') else: contact_form = ContactForm(request.POST) context = { "contact_form": contact_form, } template = 'contact.html' return render(request, template, context) 

gmail server

 EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = ' xxx@gmail.com ' EMAIL_HOST_PASSWORD = 'xxxx' EMAIL_PORT = '587' EMAIL_USE_TLS = True 

terminal output

 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: adie Ugbe From: abcd@gmail.com To: xxxx@gmail.com Date: Sun, 07 Jan 2018 11:11:10 -0000 Message-ID: < 20180107111110.29855.10393@1.0.0.127.in-addr.arpa > oh oh no ooo dddddddd se skan ------------------------------------------------------------------------------- Successful 
+5
source share
4 answers

When I set it up. He sends me an email successfully. I made a code, look at it. Follow the link for the code: https://www.dropbox.com/s/1ouq5mklq5t51su/mail.zip?dl=0

settings.py:

 INSTALLED_APPS = [ 'user', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] STATIC_URL = '/static/' EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = ' xxxxx@gmail.com ' EMAIL_HOST_PASSWORD = 'xxxxxxx' EMAIL_PORT = '587' EMAIL_USE_TLS = True 

views.py:

 from django.shortcuts import render,redirect from user.models import * from django.core.mail import send_mail from user.forms import * def contact(request): if request.method == 'POST': contact_form = ContactForm(request.POST) if contact_form.is_valid(): name = contact_form.cleaned_data['name'] message = contact_form.cleaned_data['message'] sender = contact_form.cleaned_data['sender'] phone = contact_form.cleaned_data['phone'] cc_myself = contact_form.cleaned_data['cc_myself'] recipients = [' xxxxx@gmail.com '] if cc_myself: recipients.append(sender) send_mail(name, message, sender, recipients) contact_form.save() print('Successful') return redirect('contact') else: print('Fails') else: contact_form = ContactForm(request.POST) context = { "contact_form": contact_form, } template = 'contact.html' return render(request, template, context) 

models.py:

 from django.db import models class Contact(models.Model): name = models.CharField(max_length=100) message = models.TextField() sender = models.EmailField() phone = models.CharField(max_length=10) cc_myself = models.BooleanField(blank=True) time = models.DateTimeField(auto_now_add=True, db_index=True) def __str__(self): return 'Message for {}'.format(self.sender) 

contact.html:

 <form method="post"> {% csrf_token %} {{contact_form.as_p}} <input type="submit" name="" value="submit"> </form> 
0
source

First, you are missing EMAIL_BACKEND

 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
+2
source

From your output, you can clearly see that the email is being registered on the console, which means the sending process is simulated on the console. This technically means sending email.

Anyway, I suspect this is because you have an email backend installed

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

What you need to do is change the email backend to:

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

+1
source

Setting up Django is a trick!

Django sends letters to the background job, and you must correctly set these ENV VARs if you want to use an external task manager as well.

-1
source

Source: https://habr.com/ru/post/1274385/


All Articles