Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 30029

Contact form sending emails from and to the same email -Django

$
0
0

So I am building a three-section webpage and one of these sections contains a contact form for users to send messages to my email (EMAIL_HOST_USER in settings.py). But send_mail(subject, message, email_from, [email_to,], fail_silently=False) is only using EMAIL_HOST_USER for both the sender and the recipient parts and it sends the email from and to that email. And I can't figure out what I am missing here.

Here's the code and thank you in advance.

views.py

from django.shortcuts import render
from django.http import HttpResponse
from hajar.settings import EMAIL_HOST_USER
from django.core.mail import send_mail
from .forms import ContactForm 

def base_view(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = "Hello"
            name   = form.cleaned_data['name']
            email_from =  form.cleaned_data['email']   
            email_to = EMAIL_HOST_USER   
            message= form.cleaned_data['message']
            send_mail(subject, message, email_from, [email_to,], fail_silently=False)
            return HttpResponse(f'Thank you for your message, {name}!')
    form = ContactForm()
    return render(request, 'base.html', {'form': form})

settings.py

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'

forms.py

from django import forms

class ContactForm(forms.Form):
    name    = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'mdl-textfield__input required', 'id':'name'}))
    email   = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'mdl-textfield__input required', 'id':'email'}))
    message = forms.CharField(widget=forms.Textarea(attrs={'class':'mdl-textfield__input required', 'id':'textarea'}))

Viewing all articles
Browse latest Browse all 30029

Trending Articles