Skip to content

Instantly share code, notes, and snippets.

@hersonls
Created September 30, 2012 17:42
Show Gist options
  • Save hersonls/3807862 to your computer and use it in GitHub Desktop.
Save hersonls/3807862 to your computer and use it in GitHub Desktop.
Example: Using signals to send email
# I have not tested, only didactic.
#
# signals.py
#
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
def send_notification_mail(sender, instance, raw, using):
context = {}
subject = "Subject"
text_content = "Text content"
from_email = "admin <admin@mydomain.com>"
to = [instance.email]
html_body = render_to_string('email.html', context)
mail = EmailMultiAlternatives(subject, text_content, from_email, [to])
mail.attach_alternative(html_body, "text/html")
mail.send()
# models.py
#
from django.db import models
from signals import send_notification_mail
class PeopleModel(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
models.signals.pre_save.connect(send_notification_mail, sender=PeopleModel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment