Python script sends bulk email

Many organizations need to send e-mail to all their members now and then. This script sends one copy of the original e-mail to each member.  E-mail clients can send copies of the same message to a bunch of people. However, many mail servers will filter out such bulk transmissions and classify them as spam. So, to increase the probability of the e-mail reaching the recipients the mail must be sent many times with only a few recipients each time. Usually this is done by sending the same mail over and over and for each paste a part of the member list into the to or copy-to-field. If you have two hundred members and choose to send to ten of them each time this procedure must be repeated 20 times.

Bulky

I wrote bulky.py, a Python script, that runs on your computer and sends an e-mail to all recipients of a recipient list. The work flow is illustrated in the following diagram

bulky work flow

  1. First, create the e-mail you want to send, including images, attachments etc.
  2. Send this e-mail to a special address, in this case bulky@example.com
  3. Then run the script on your computer. The script reads a configuration file bulkycfg.py, fetches the mail for bulky and then sends this new e-mail to you
  4. You check your e-mail and verify that the e-mail you got looks right
  5. Then you run the bulky.py again, this time with the recipient list file as a command line parameter
  6. Bulky.py now transmits the e-mail to all recipients according to the list

Prerequisites

First, you need access to an e-mail account which you can fetch mail from using the POP-protocol. If you have an account with an internet service provider, or e.g. google mail, you usually can set up POP access. Likewise, you need an SMTP account you can use to send the e-mails.

On your computer you must have the programming system Python installed. See python.org to find out how to download and install Python. You want the newest version in the 2-series. Python is free.

You also need my script, which can be downloaded here. This is a zip-file that can be unzipped somewhere in the work area of your computer.. After unzip you will have a new folder containing the files bulky.py, bulkycfg.py, listrecipients.py and popdelete.py. I’ll come back to these later.

Setup

You need a special e-mail account which you use for this purpose only. As an example I use bulky@example.com here. You also need an SMTP account, which you probably already have and use to send your ordinary e-mails. For both accounts you need the server name, user name, password and port number. You can find an example configuration file in the example folder.

The bulkycfg.py file contains all the setup parameters, like server names and passwords. This file must be edited to suit your situation. For editing you need a programmer’s editor or any editor that doesn’t insert any formatting data into the file. Notepad, nano, are OK examples. You can not use a text editor like Word or Open Office.

You will also need the recipient list file. You can name that file whatever you want (don’t use spaces though), for this example I use reclist.txt. This is also a plain text file, so as for the configuration file you must use a programmer’s editor. The file lists all the recipients email address, one address per line. You can use the address only, like ‘oao@oao.no’ or the compound address  ‘O. Olsen<oao@oao.no>’.Instead of an editor you may also use a one column spread sheet, one address in each cell and then export it as a csv-text file. bulky.py cannot read a proper spread sheet file. You can use the listrecipients.py program to check if your list is correctly interpreted, open a command window and type

python listrecipients.py reclist.txt

Any suspicious address will be tagged by ‘**** error?’

You can have several such files for different purposes, e. g. members sorted geographically, so when you want to only reach members in a certain region you use a list for that region.

Rate limiting

As you know, computers are often used to send spam. Many mail servers your mail may encounter on the way from bulky.py to the recipients will filter out messages that come in large number bursts from the same sender and label them as spam. Your service provider may also set limits for the minimum interval between each. You can find these limits by contacting your provider or read their documentation.   As an example, one provider specifies these limits: max 1 message each second, max 60 messages each minute, max 1500 messages each hour and max  5000 each day.

So if you want to send 100 e-mails and want to stay a bit off the limits you can use two minutes for this. The time interval is then 2*60/100=1.2 sec. If you want to send 2000 e-mails, you may want to spend two hours which yields  2000/(2*3600)= 0.28 e-mails/sec, or 2000/(2*60)=  17 /min or 2000/3=1000/hour. All these rates are well within the limits. In this case the interval is 1/0.28=3.6 sec.

When you have calculated a suitable rate for your bulk mail you must set that in the configurations file.

 Let’s do it

So you want to send bulk mail to your members. First you have to create the original e-mail which will act like a template. You do that with your e-mail client as usual. Then you send this to your bulky-account, I this case I use  bulky@example.com.

Now, on your computer you can check if the e-mail has reached the destination by running in a command window the command

python popdelete.py

This will list all messages awaiting at the server. It should be only one. Hit the return key to exit the program. If your template does not show up as the first one you have to delete the others. You do that by keying in the number of the unwanted messages separated by spaces.

When your template is mail nummer 1 on the server you can run the command

python bulky.py

If everything is OK this program will download your e-mail template and then send it to the address defined by DEF_TO in the configuration file. Usually that will be you. So, after  a few seconds you can check your mail and read this test e-mail.

If things went well you can commit the bulk transmission by the command

python bulky.py reclist.txt

The  parameter is the recipient list file name, in this case reclist.txt. You will now see the recipients e-mail addresses listed as their mails are sent.

When all e-mails have been sent ant bulky.py terminates you can run popdelete.py again to delete the e-mail template from the server.

 

2 Responses to “Python script sends bulk email”


  • hey, i do not understand the usage of special email bulky@example.com.
    do i need to setup another valid mail account and send my template to that or just send to some fake address??

  • For simplicity I use a special mail account for this so there will be only a single mail on the pop.server. I think it will be easy to change the bulky.py to load an indexed mail from your regular account. Use popdelete.py to get the index and then read the index number in bulky.py and download the email .

Leave a Reply