1

I want to send emails in django rest framework, for example when someone creates an account,or comments something and etc. I found this in django documents. But I don't know where to use it. maybe in Viewset but in which method.

so what is the best way and where is the best place to send emails in Django Rest Framework?

Hamid
  • 503
  • 6
  • 22

3 Answers3

2

I would like to give you couple of suggestions(Of course, with reasons):

  1. That is synchronous. Maybe that would be not desired/efficient. I would suggest you to use something like this package (django-anymail) , this will decouple your email sender. Assume now you're using AWS SES, tomorrow you want to switch to sendgrid, then you'll only need to change setting variables with this.

  2. Assuming User, Comment each of these are separate models. I would suggest you to override the save() method of these models and send email from there. I am not suggesting signals because I feel (fully personal view) that signals are to be used when access to the source is not possible, like some event in 3rd party library, etc.

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
1

Assuming that "Creating an account" and "commenting on something" both create some model instance, you could use existing model signals.

If you need to catch events that don't already send a signal, you will have to define new signals and send them from the appropriate place (which depends on what you're interested in so no 'one-size-fits-all' answer here).

Also if you have more than one mail to send at a time you may want to consider using some async task queue to send the emails - this avoids delaying the response (django signals are NOT async).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Thank you for great suggstions. I will use save method from models and django-anymail for sending more than one email. – Hamid Oct 14 '18 at 05:02
0

You can register sending function to some django signals or You can call it in Class-based Views or Function Based Views.

Hayden
  • 449
  • 4
  • 13