0

Adding BroadCast Mail to All User Registered in Django Admin. This is my last problem, we need custom default Django Admin to can submit BroadCast mail to All User. Because this is perfectly to make a promotions.

This problem has been helped by our Danny W. Adair who are answered someone’s question about the “Django Admin Customizing”.

Makesure in your settings.py was configured for your email backends, egg like this:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your_email@gmail.com'
EMAIL_HOST_PASSWORD = 'your_password_email'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

1. In your models.py

class BroadCast_Email(models.Model):
    subject = models.CharField(max_length=200)
    created = models.DateTimeField(default=timezone.now)
    message = RichTextUploadingField()

    def __unicode__(self):
        return self.subject

    class Meta:
        verbose_name = "BroadCast Email to all Member"
        verbose_name_plural = "BroadCast Email"
  1. In your admin.py, importing some module for “admin” and for “email setup”.
from django.contrib import admin
from django.utils.safestring import mark_safe
import threading
from django.conf import settings
from django.http import HttpResponse
from django.core.mail import (send_mail, BadHeaderError, EmailMessage)
from django.contrib.auth.models import User

class EmailThread(threading.Thread):
    def __init__(self, subject, html_content, recipient_list):
        self.subject = subject
        self.recipient_list = recipient_list
        self.html_content = html_content
        threading.Thread.__init__(self)

    def run(self):
        msg = EmailMessage(
                self.subject,
                self.html_content,
                settings.EMAIL_HOST_USER,
                self.recipient_list
        )
        msg.content_subtype = "html"
        try:
            msg.send()
        except BadHeaderError:
            return HttpResponse('Invalid header found.')

class BroadCast_Email_Admin(admin.ModelAdmin):
    model = models.BroadCast_Email

    def submit_email(self, request, obj):
        # `obj` is queryset, so there we only 
        # use first selection, exacly obj[0]

        # this for exception: `if p.email != settings.EMAIL_HOST_USER`
        list_email_user = [ p.email for p in User.objects.all() ]
        obj_selected = obj[0]
        EmailThread(
            obj_selected.subject,
            mark_safe(obj_selected.message), 
            list_email_user
        ).start()

    submit_email.short_description = 'Submit BroadCast (1 Select Only)'
    submit_email.allow_tags = True

    actions = [ 'submit_email' ]
    list_display = ("subject", "created")
    search_fields = ['subject',]


admin.site.register(models.BroadCast_Email, BroadCast_Email_Admin)

3. And then, you can see. we have “Submit BroadCast” selection, just click button “Go” to submit broadcast mail.

Adding BroadCast Mail to All User Registered in Django Admin

tricks python django

Your Answer

blog comments powered by Disqus