from django.db import models from django.utils.translation import ugettext as _ from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver from django.core.mail import EmailMultiAlternatives from musicians.models import Musician from django.template.loader import get_template from django.template import Context class Message(models.Model): titel = models.CharField(max_length=100, verbose_name=_("titel")) text = models.TextField(blank=False, verbose_name=_("text")) creation_time = models.DateTimeField(verbose_name=_("creation_time")) author = models.ForeignKey(User, verbose_name=_("Author"), on_delete=models.PROTECT) def __unicode__(self): return self.author.username + " : " + self.titel @receiver(post_save, sender=Message) def my_handler(sender, instance, created, **kwargs): if not created: return receivers = [m.user.email for m in Musician.objects.all()] subject = "Blechreiz Forum: " + instance.titel from_email = 'forum@blechreiz.com' c = {'messages': Message.objects.all().order_by('-creation_time')[:10]} text_template = get_template("simpleforum/mail.txt") # html_template = get_template( "simpleforum/mail.html" ) text_content = text_template.render(Context(c)) # html_content = html_template.render( Context(c) ) msg = EmailMultiAlternatives(subject, text_content, from_email, receivers) # msg.attach_alternative( html_content, "text/html" ) msg.send()