how to break an email list into manageable sizes for emailing

24 views
Skip to first unread message

MikeKJ

unread,
May 2, 2014, 12:08:49 PM5/2/14
to django...@googlegroups.com
    def send( self ):
        c = Context({ "content": self.introductory_text, "user":None, "request":None, "updates": [] })#Section.updates.all()[:20] })
        t = loader.get_template('emailer/html/updates.html')
        subject = self.subject
        recipients = []
        if self.to_all_principal_contacts:
            self.add_principal_contacts()
        if self.to_all_subscribers:
            self.add_subscribers()
        for i in self.recipients.all():
            recipients.append( i.email )
        self.save()
        html = t.render(c)
        for i in recipients:
            send_html_email( subject, html, "emailataddress.com", i, image_root=settings.PROJECT_DIR )
        from datetime import datetime
        self.sent_on = datetime.now()
        self.sent = True
        self.save()

What I want to do is break recipients[] into chunks of say 100 and then send_html_email on each email address within each chunk for all chunks BUT I want to preserve the self.sent = True self.save() to be done after all chunks have been sent.
I think it may also need some method of timebreak between each chunk being processed.
This is come about because the server/host seems to have a problem with sending 1500 emails in one go.  The above sends to small numbers but not all in one go.

Thanks for helping





James Schneider

unread,
May 3, 2014, 3:45:22 AM5/3/14
to django...@googlegroups.com
This task would be better left to a background batch processor such as Celery, which integrates nicely via the Django-celery package. Submitting the form would place a job into the Celery queue, and within that job you can pull 100 (or however many you need, see https://docs.djangoproject.com/en/1.6/topics/db/queries/#limiting-querysets) addresses at a time in a loop and send them out, waiting for each chunk to complete before sending the next set. 

This also keeps your users from waiting for the email sending task to complete before returning a response, otherwise the page will 'hang' after submission while all of the emails are being sent, which may or may not be acceptable for your user base, but I would caution against it given other constraints such as browser and connection timeouts, not to mention holding up resources and worker threads on your web server from being available to other users.

-James
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fc804755-1bc1-43f0-a15f-a98984be04fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages