Email Notification with Signals
A snippet showing how to send emails on post_save with signals.
from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Comment
@receiver(post_save, sender=Comment)
def send_email_on_comment(sender, instance, created, **kwargs):
if created:
send_mail(
'New Comment',
f'A new comment was added: {instance.text}',
'[email protected]',
['[email protected]']
)
Explanation:
-
A
post_savesignal is triggered when a newCommentis added. -
Sends an email automatically when a new
Commentis added.
- Category Signals
- Total Views 897
- Last Modified 13 May, 2026
- Tags #signals #email #notification #automation
Previous snippet