Question

Is there a way to use the django-contact-form to send a html email?

Was it helpful?

Solution

I just looked up the source and it's not built in.

You can certainly modify it however you please -- it's a very simple form and very readable.

You'd need to add a html_template variable/path and modify the save line, and add a way to specify which template to use.

def save(self, fail_silently=False):
    """
    Build and send the email message.

    """
    data = self.get_message_dict()
    msg = EmailMultiAlternatives(subject=data['subject'], body=data['message'], from_email=data['from_email'], to=data['recipient_list'])

    msg.attach_alternative(loader.render_to_string('path_to_my_html_template.html', self.get_context()), "text/html")
    msg.send(fail_silently=fail_silently)

This is just easier to keep all of your changes in one spot.

Otherwise, you could re-write most of the code and change the method names and the strings in get_message_dict() to reflect the new field names for EmailMessage

Frankly, i'm not sure why send_mail should have different keywords than EmailMessage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top