How to add a random time between two emails sent from zope.sendmail

Ecrit le 15 Jun 2010

I’m using collective.dancing for a mass mailing: +8000 emails to send.

One of the question is how to not be blacklisted ? A first thing I have done today is to add a random time between two sent.

To achieve this I have read the code of zope.sendmail. It is a well documented egg so this is not hard to understand how it works:

  • A maildir (a directory) store mails under files ready to be sent
  • A mailer (a utility) is ready to be called to send email one by one
  • A queue mail delivrery (utility) is ready to put messages inside a maildir
  • A mailqueue processor (not a component, a thread implementation) parse every 3 seconds if there is new emails to send in the mailbox

So to achieve this you just have to unconfigure the plone.smtp utility and declare a new utility called plone.smtp with this code:

import random
import time

from zope.sendmail import mailer

class SMTP(mailer.SMTPMailer):
    """Override SMTPMailer to let a random time"""

    def send(self, fromaddr, toaddrs, message):
        time.sleep(random.uniform(0,2))
        super(SMTP, self).send(fromaddr, toaddrs, message)

It’s working !