ubernostrum/django-contact-form

Consider using EmailMessage instead of send_mail

Closed this issue · 4 comments

Django's send_mail function doesn't allow for things like cc, bcc, and reply_to. Being able to easily use those with django-contact-form would be very nice. Currently, using those requires overriding the ContactForm class' save method. Although this change wouldn't be backwards compatible, I think it would be a good change.

It's unlikely that this will happen in django-contact-form.

@ubernostrum OK. Thanks for the response. Perhaps I should have said this to begin with, but my main motivation for submitting this issue was for reply_to. In the projects I work on, the site owner generally wants to be able to reply to the contact form emails and have it go to the person who filled out the contact form. If that doesn't change your mind, that's fine; I just wanted you to be aware of that use case in case it does change your mind.

Just to leave some feedback here. The reply_to field would be very useful here as I think most people using the form will want to directly reply to the person who filled out the form.

For anybody else who might be looking to add Reply-To, the following worked for me:

from django.core.mail import EmailMessage
from contact_form.forms import ContactForm


class MyContactForm(ContactForm):
    def save(self, fail_silently=False):
        '''
        Build and send the email message with reply-to header set
        '''
        
        md = self.get_message_dict()
        reply_to = self.get_context()['email']
        
        email = EmailMessage(
            subject=md['subject'],
            body=md['message'],
            from_email=md['from_email'],
            to=md['recipient_list'],
            reply_to=[reply_to],
        )
        
        email.send(fail_silently=fail_silently)