I'm looking for a way to send bulk emails to users from a Django Admin Action. This is what I have thus far:
class MyUserAdmin(UserAdmin):
list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', staff]
list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active']
actions = ['send_EMAIL']
def send_EMAIL(self, request, queryset):
from django.core.mail import send_mail
for i in queryset:
if i.email:
send_mail('Subject here', 'Here is the message.', '[email protected]',[i.email], fail_silently=False)
else:
self.message_user(request, "Mail sent successfully ")
send_EMAIL.short_description = "Send an email to selected users"
This is fine but! I have to hardcode the actual message every single time. What if I could make it Dynamic? Instead of changing the message from the admin.py every single time I need to send a bulk email, why not create an intermediate Django admin action page that has a empty Text input field where I can write a new message to send every single time?
How can this be done? I'm looking for a well detailed answer that is not open ended and generic.
You are on the right track. Here is my implementation of a django admin action that allows you to write a message to the selected users. (I know this is super late but might help someone else).
send_email function:
send_email.html template (I borrowed the markup from the django confirm delete view for this you may want to do something different here):
send email form class:
And finally the send email view + url conf:
This implementation worked fine for me. Here is what the intermediate view looks like:
You might have to change a couple of things in the template where I build out the breadcrumbs or the reverse url for the view in case you don't have an app called
users
or a model calledUser
.