Faziki
Faziki

Reputation: 821

Reply to Inputted Email Django

I have a contact form in my website and Its working but in my email setup, I'm using a static email and wish to use the email that was typed in the email field instead.

The data grabbed from the form looks like this

{'from_email': '[email protected]', 'subject': 'Hello World', 'message': 'asdasdasd'}

My imports in my view.py file

from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from django.conf import settings
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import ContactForm
from django.core.mail import EmailMessage

The class view looks like this


class ContactMe(View):
    template = "contact.html"
    context = {
        'form': ContactForm()
    }

    def get(self, request):
        print("getting form")
        return render(request, self.template, self.context)

    def post(self, request):
        form = ContactForm(request.POST)

        def __init__(self):
            self.mailSender = mailSender()
            Mail = POST
            Mail.MailSender.send_mail()
        if form.is_valid():
            data = form.cleaned_data
            print(data)
            send_mail(data['subject'], data['message'],
                      settings.EMAIL_HOST_USER, data['from_email'])
     next = request.POST.get('next', '/')
            return HttpResponseRedirect(next)`

the data['from_email'] was set to my email ['[email protected]'] and I changed it to the label name in the data that it gets ('from_email')

It works 100% with a static email instead of the variable form_email but using form_email gives an error

TypeError: "to" argument must be a list or tuple

I then changed it to

to=['from_email']

but then I got this error TypeError: send_mail() got an unexpected keyword argument 'to'

Reason for this is I want the person's email to be with the subject and message to my email so that I can directly reply from my inbox

Any Ideas?

Kind Regards,

Upvotes: 0

Views: 2491

Answers (1)

dirkgroten
dirkgroten

Reputation: 20692

You cannot fake the "from" address of an email by trying to set it to data['from_email'], that's not allowed by most SMTP providers (they'll replace it back with the real "from"). But you can set the "reply-to" address (which will show up in your email client and allow to rapidly reply to the correct address).

To do that, instead of using send_mail(), create a EmailMessage or EmailMultiAlternatives, set the reply_to address and send() it:

from django.core.mail import EmailMultiAlternatives

message = EmailMultiAlternatives(
    data['subject'], 
    data['message'], 
    to=["[email protected]"],  # where you receive the contact emails  
    from_email=settings.DEFAULT_FROM_EMAIL,
    reply_to=[data['from_email']])
message.send()

Upvotes: 3

Related Questions