0

i have a user model where after creating the model I want to send email from signal.

I have a function in my signals.py

def notify_after_registration(sender, instance, created, **kwargs):
    user = instance.username
    email_subject = 'Account confirmation'
    email_body = "some message"            
    send_mail(
        email_subject, 
        email_body, 
        settings.EMAIL_HOST_USER,
        [instance.email, 
        fail_silently=False
    )

at the bottom i have

post_save.connect(notify_after_registration, sender=User)

When I register the user user is saved but email is not sent.. What is wrong ?

Granitosaurus
  • 20,530
  • 5
  • 57
  • 82
gamer
  • 5,673
  • 13
  • 58
  • 91
  • Quite a few things could be wrong here... Did you at least try calling the `notify_after_registration` function manually (from the Django shell) ? If yes, what did you get ? (and if no, you know what you have to do...) – bruno desthuilliers Jul 03 '15 at 11:40
  • `signals.py` is not automatically imported by Django, it could be that your receiver never gets connected, see http://stackoverflow.com/a/29698337/202168 – Anentropic Jul 03 '15 at 11:44
  • I miss some considerations: Have you verified if function is called? Have you change fail_silently to true to isolate issue? Is a typo snipped code bad indentation send_mail line ? – dani herrera Jul 03 '15 at 12:11
  • Would be simpler and bug free to just override the models save method and send the email from there. – elssar Jul 03 '15 at 12:45
  • `[instance.email` is missing a closing bracket too. – Granitosaurus Dec 20 '18 at 04:41

1 Answers1

0

the code from your signals.py by default is not imported anywhere, so you need to add it import in some part of your app, for example in file with models (or, one of models if your have many such files)

Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88