'Send an email if to a user registering in django when admin changes their account to is_active=True status
I am creating a register and login system in a project whereby on registering a users default is is_active=False. The admin then receives an email on user registering to approve or leave it inactive. on changing users status to is_active=True, the user should receive an email informing them they can now login.But i am not receiving any emails..or emails being sent, is there an error in my codes am failing to see??
Here is my signals.py
from .models import AuthUser
from django.db.models import signals
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from django.conf import settings
from django.core.mail import send_mail
#signal used for is_active=False to is_active=True
@receiver(pre_save, sender=AuthUser, dispatch_uid='active')
def active(sender, instance, **kwargs):
if instance.is_active and AuthUser.objects.filter(pk=instance.pk, is_active=False).exists():
subject = 'Active account'
message = '%s your account is now active' %(instance.username)
from_email = settings.EMAIL_HOST_USER
send_mail(subject, message, from_email, [instance.email], fail_silently=False)
#signal to send an email to the admin when a user creates a new account
@receiver(post_save, sender=AuthUser, dispatch_uid='register')
def register(sender, instance, **kwargs):
if kwargs.get('created', False):
subject = 'Verificatión of the %s account' %(instance.username)
message = 'here goes your message to the admin'
from_email = settings.EMAIL_HOST_USER
send_mail(subject, message, from_email, [from_email], fail_silently=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
