views:

223

answers:

1

Is there an existing plug-in to produce daily or weekly digest emails in Django? (We want to combine many small notifications into one email, rather than bother people all the time.)

Django-mailer claims to support this, but I'm told it doesn't really.

+2  A: 

There is django-mailer app of which I was not aware till now, so the answer below details my own approach.

The simplest case won't require much:

put this into your app/management/commands/send_email_alerts.py, then set up a cron job to run this command once a week with python manage.py send_email_alerts (all paths must be set in the environment of course for manage.py to pick up your app settings)

from django.core.management.base import NoArgsCommand
from django.db import connection
from django.core.mail import EmailMessage

class Command(NoArgsCommand):
    def handle_noargs(self,**options):
        try:
            self.send_email_alerts()
        except Exception, e:
            print e
        finally:
            connection.close()

    def send_email_alerts(self):         
        for user in User.objects.all():
            text = 'Hi %s, here the news' % user.username
            subject = 'some subject'
            msg = EmailMessage(subject, text, settings.DEFAULT_FROM_EMAIL, [user.email])
            msg.send()

But if you will need to keep track of what to email each user and how often, some extra code will be needed. Here is a homegrown example. Maybe that's where django-mailer can fill in the gaps.

Evgeny