I'd like to trigger a simple plaintext email to admin(s) when a new user registers for my Django app. What's the cleanest way to do this?
You can use a post save signal for this. For example:
# <app>/signals.py:
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 send_mail
@receiver(post_save, sender=User)
def send_email_to_admin(sender, instance, created, **kwargs):
if created:
send_mail(
'<Subject>User {} has been created'.format(instance.username),
'<Body>A new user has been created',
'from@example.com',
['admin@example.com'],
fail_silently=False,
)
# <app>/apps.py
from django.apps import AppConfig
class YourAppConfig(AppConfig):
name = 'app_name'
verbose_name = _('app_name')
def ready(self):
import .signals # noqa
Here, I am using django's mail sending functionality
as example, if you are using that then please make sure you have properly configured the settings.