Comment email notification in Django 1.0 on Webfaction

It's always nice to know when someone posts a comment on your blog, and if you're using something like Wordpress then email notification is already built in. Well, you guessed it, I'm not using Wordpress :)

The code

I got the original code to hook into signals sent from the comment system from here, but it's not compatible with 1.0. Thankfully, the fix is easy:

 
from django.contrib.comments.models import Comment
from django.core.mail import send_mail
from django.db.models.signals import post_save

def comment_notification(sender, instance, **kwargs):
    subject = 'New Comment on %s' % instance.content_object.title
    msg = 'Comment text:\n\n%s' % instance.comment
    send_mail(subject, msg, 'from@mysite.ca', ['mail-to-me@mysite.com']) # ideally these go in the settings 
    
post_save.connect(comment_notification, sender=Comment)

There were minimal modifications to make the code work from the original version, now the next trick is to get Webfaction to send my email.

Webfaction and send_mail

Wefaction won't just let you send mail right off the bat, you need to set up a mailbox and email address on your Webfaction account, and then change a couple settings in your project's settings.py file. I had found a forum post with the details but they implied I should be using 'mailx.webfaction.com' (x being my mail server number), but it wouldn't work. I recalled after that my outgoing mail server is 'smtp.webfaction.com'.

EMAIL_HOST='smtp.webfaction.com' 
EMAIL_HOST_USER='mymailbox' # < this should be the mailbox
EMAIL_HOST_PASSWORD='mymailboxpass' # < a valid password (obviously)
EMAIL_PORT='25'
DEFAULT_FROM_EMAIL = "my-email@my-webfaction-account.com" # < the next 2 are the email address tied you your mailbox
SERVER_EMAIL = "my-email@my-webfaction-account.com"

And bada-bing! The mail comes through snappy fast too. Thank goodness for Django, black licorice, and Webfaction :)

Cheers, Aaron

Posted

Oct 10, 2008
by AnonymousUser

Categories

General Posts