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
Got something to say?
Our Products
Categories
- accessiblity
- code
- company news
- django
- gondola
- open source
- portfolio
- presentation
- pro tip
- review
- screencast
- seo
- software
- subversion
- trailmapping
- wordpress
Archives
- July, 2010
- June, 2010
- May, 2010
- April, 2010
- February, 2010
- December, 2009
- November, 2009
- October, 2009
Elsewhere
What we’ve been up to online
-
Just launched a Flask/App Engine mini-site we've been tinkering on http://emailed-me.appspot.com/
Pete, 14 hours, 36 minutes ago -
created repository Emailed-Me-
Pete, 14 hours, 43 minutes ago -
Our first iPhone development project hit the App Store last week and is already over 1k users! Check them out @takemyspot #iphone #geodjango
Pete, 3 weeks ago -
Love the new sites! RT @welikesmall: We just launched two new sites. http://post.ly/mGoq
Pete, 3 weeks, 1 day ago -
Pro tip: Using pip safely for automated deployment (no more pesky prompts) http://bit.ly/b5zsPa
Pete, 4 weeks, 1 day ago -
commented on justquick/django-mailfriend
Pete, 1 month ago -
RT @unbracketed: Excited to have @mitsuhiko joining us for some work this summer :)
Pete, 1 month ago -
New blog post: managing supervisord with upstart http://bit.ly/db3p5N
Pete, 1 month ago -
Troubleshooting OpenID is just like user/password. Except you have 5 of them and and you don't know which one is failing, and 3 login pages
Pete, 1 month, 1 week ago -
This gets very interesting around 42 min. Using javascript to snoop inside firewalled networks http://bit.ly/aNVPc5
Pete, 1 month, 2 weeks ago -
The final tally is in. 8 Lincoln Loopers attending DjangoCon. 3 US, 4 EU, and 1 NZ. Looking forward to it!
Pete, 1 month, 2 weeks ago -
Twitter / Dustin Curtis: I'm flying to Madrid tomor ...
Dustin Curtis travels to Berlin, Bangkok & Madrid in exchange for design services as the result of a late night tweet.
Pete, 1 month, 2 weeks ago -
created branch ubuntu-8.04 at lincolnloop/fab-pave
Pete, 1 month, 3 weeks ago -
created repository fab-pave
Pete, 1 month, 3 weeks ago -
pushed to master at lincolnloop/django-mailfriend
Pete, 1 month, 3 weeks ago


See also http://code.google.com/p/django-contact-form/
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?
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.
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.
In my opinion you are not right. I am assured.
31924