Created
September 30, 2012 17:42
-
-
Save hersonls/3807862 to your computer and use it in GitHub Desktop.
Example: Using signals to send email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 <[email protected]>" | |
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