Django FormMail Clone

Here’s a little nugget I just posted to Django Snippets. It emulates the behavior of an old Perl script I used way back when, FormMail.pl.

I often find myself needing to build a form whose contents get emailed to the site owner(s). This class let’s you call form.notify() on any form that is a subclass of it to have the fields ordered and sent in a plain text email to all users that are flagged as staff.

from django import newforms as forms
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail

class FormMail(forms.Form):
    def notify(self):
        """
        Sends an email to all members of the staff with ordered list of fields
        and values for any form that subclasses FormMail
        """
        site_name = Site.objects.get_current().name
        form_name = self.__class__.__name__
        subject = '%s %s Submission' % (site_name, form_name)
        user_list = [u.email for u in 
                    User.objects.filter(is_staff=True).exclude(email="").order_by('id')]
        message = ""
        for k in self.base_fields.keyOrder:
            message = message + '%s: %s\n\n' % (self[k].label, self.cleaned_data[k])
        send_mail(subject, message, user_list[0], user_list)
        
## Example form
class ContactForm(FormMail):
    name = forms.CharField()
    phone = forms.CharField(required=False)
    email = forms.EmailField()
    comment = forms.CharField(widget=forms.Textarea())

## Example view code
form = ContactForm(request.POST)
if form.is_valid():
    form.notify()

Comments

  • March 25, 2008 at 2:14 p.m. #
    James Bennett piped up:

    See also http://code.google.com/p/django-contact-form/

  • March 25, 2008 at 2:30 p.m. #
    Pete mentioned:

    Thanks James, I didn’t notice your project until after I had already written my code.

    I haven’t dug into django-comment-form, what extra goodies does yours offer?

  • March 25, 2008 at 3:20 p.m. #
    Rob Hawkins responded:

    I’m just a user of James’ contact-form, but what drew me to it was akismet. It’s a subclass(?) of the default ContactForm class. Just need to add your akismet key to settings.py and point the contact form to AkismetContactForm and voila.

  • March 25, 2008 at 9:10 p.m. #
    Brian Luft chimed in with:

    Whoops – I just broke out the contact form on my site into a separate app… based on a comment by James during his talk at PyCon… not realizing that this existed. He probably mentioned it but I was typing away.
    I like this implementation as well – very slick! Thanks for sharing.

  • September 9, 2009 at 1:54 p.m. #
    MichelleJD chimed in with:

    In my opinion you are not right. I am assured.

    31924

Got something to say?





Our Products

Gondola
Gondola is a hosted CMS that gives designers a powerful and easy platform to create amazing geo-enabled Websites.
Trailmapping
Still in development, Trailmapping is a GPS enabled trail guide and trip logger.

Categories

Archives

Elsewhere

What we’ve been up to online

Interested in working with us?
Fill out the form below or contact us at:

PO Box 774441
Steamboat Springs, CO
80477

ph: 970.879.8810
fx:  970.367.8596
info@lincolnloop.com