Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django send email - email form
#1
Hello, I created a contact form to send me an email when the user fills it out. Everything appears to be working, I get mail to my gmail but I get it from myself, not from filled "email form".

For example I fill out my form in my website:

Subject: TEST
Email: [email protected]
Message: Hello

When I check my gmail account I got my message:

from Email: [email protected] - ?
to Email: [email protected]
date: 17 Sep 2020, 14:05
subject: TEST
mailed-by: gmail.com
Message: Hello

my forms:
class EmailForm(forms.Form):
subject = forms.CharField(max_length=50,
                          widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Your subject'}))
email  = forms.EmailField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Your email'}))
message = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Your message'}))
my views:
def contact(request):

form = EmailForm()

if request.method =="POST":
    form = EmailForm(request.POST)
    if form.is_valid():
        subject = request.POST['subject']
        message = request.POST['message']
        email = request.POST['email']
        send_mail(
            subject,
            message,
            email,
            ['[email protected]'],
            fail_silently=False,
            )
        context = {'subject':subject}
        return render(request, 'base/contact.html', context)

context = {'form':form}
return render(request, 'base/contact.html', context)
my settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Reply
#2
It seems that the problem is in the google mail server, which is sneaky enough to always use EMAIL_HOST_USER (user that is used for authentication) instead of the from_email value.

The solution is to use a different mail server like sendgun or mailgrid (haven't used any of those so don't know)
Reply
#3
Thanks for answer. I tried with yahoo but I got server error.

So I change my views:
def contact(request):
	
	form = EmailForm()

	if request.method =="POST":
		form = EmailForm(request.POST)
		if form.is_valid():
			subject = request.POST['subject']
			message = request.POST['message']
			email = request.POST['email']
			send_mail(
				subject,
				email +'\n\n'+ message,
				'[email protected]',
				['[email protected]'],
				fail_silently=False,
				)
			context = {'subject':subject}
			return render(request, 'base/contact.html', context)
Now, "sending mail" appears in my message. Maybe I don't want it, but it works. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  email scraper_help Blue Dog 6 6,700 Aug-11-2023, 01:14 PM
Last Post: snippsat
  Deployed Spider on Heroku: How do I email downloaded files? JaneTan 2 1,523 Mar-24-2022, 08:31 AM
Last Post: JaneTan
  How to send notifications to gmail from contact form using Django and pushbullet Justchse 0 1,842 Sep-01-2020, 01:19 PM
Last Post: Justchse
  Django admin form with parent child tmmaersk 0 1,836 Apr-02-2020, 06:42 AM
Last Post: tmmaersk
  Send email to gmail after user fill up contact form and getting django database updat Man_from_India 0 2,071 Jan-22-2020, 03:59 PM
Last Post: Man_from_India
  Flask email hookup for password reset Larz60+ 0 1,967 Oct-24-2019, 01:59 AM
Last Post: Larz60+
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,247 Jun-30-2019, 12:21 AM
Last Post: scidam
  How to send data from remotely hosted HTML form to Pi sajid 2 2,532 Jun-27-2019, 10:28 PM
Last Post: sajid
  sending email from admin to user in django anjana 0 3,220 Jun-07-2019, 12:01 PM
Last Post: anjana
  Django - Retrieve form data justantest 0 2,814 May-23-2019, 11:47 AM
Last Post: justantest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020