This is my third working iteration. Assuming you have an email template like this:
{% block subject %}{% endblock %} {% block plain %}{% endblock %} {% block html %}{% endblock %}
I reworked the iteration of sending email via the default list, and there are utilities for sending in one email and django.contrib.auth User (one and several). I cover, perhaps more than I need, but there you go.
Perhaps I also moved to the top with Python-love.
def email_list(to_list, template_path, context_dict): from django.core.mail import send_mail from django.template import loader, Context nodes = dict((n.name, n) for n in loader.get_template(template_path).nodelist if n.__class__.__name__ == 'BlockNode') con = Context(context_dict) r = lambda n: nodes[n].render(con) for address in to_list: send_mail(r('subject'), r('plain'), 'from@domain.com', [address,]) def email(to, template_path, context_dict): return email_list([to,], template_path, context_dict) def email_user(user, template_path, context_dict): return email_list([user.email,], template_path, context_dict) def email_users(user_list, template_path, context_dict): return email_list([user.email for user in user_list], template_path, context_dict)
As always, if you can improve it, do it.
Oli
source share