[Answered ]-Django-contact-form: How to use form submitter's email as the from_email

2๐Ÿ‘

โœ…

Ok, I took a look at the ContactForm class. The easiest way I can see to set the from_email to the email submitted in the form is to override ContactForm.get_messge_dict:

from contact_form.forms import ContactForm
from django.conf import settings

class ContactFormPublic(ContactForm):

    tuples = settings.WORKSHOP_ADMINS
    recipient_list = [mail_tuple[1] for mail_tuple in tuples]

    def get_message_dict(self):
        if not self.is_valid():
            raise ValueError("Message cannot be sent from invalid contact form")
        message_dict = {}

        """
        I removed 'from_email' from the message_part tuple check and updated the
        message_dict, setting the 'from_email' to the value of self.email
        """
        for message_part in ('message', 'recipient_list', 'subject'):
            attr = getattr(self, message_part)
            message_dict[message_part] = callable(attr) and attr() or attr
        message_dict.update({'from_email' : getattr(self, 'email')})

        return message_dict

Hope that helps you out.

๐Ÿ‘คBrandon Taylor

Leave a comment