I recently had a moderately large bulk mail I wanted to send out to many of my friends. I wanted to automate the process, but I also wanted to personalize each mail a bit by addressing it directly to individuals. I wound up writing the below Python script, which takes as input a set of contacts exported in “Microsoft Outlook CSV” format (I exported mine from Google Contacts). It generates emails according to a pattern, but doesn’t send them. Instead it saves them to the Drafts folder of your GMail. From there you can manually review, tweak, and send them. If you prefer to simply send the mail straightaway, you can modify the script to do that, but be aware that you will hit rate limits sooner.
#!/usr/bin/python import imaplib import csv import email import time srv = imaplib.IMAP4_SSL("imap.gmail.com") srv.login('email@example.com', 'password') srv.select('[Gmail]/Drafts') def send_m(first_name, address): message = email.message_from_string("""To: %s Subject: SUBJECT_GOES_HERE Dear %s, BODY_GOES_HERE """ % (address, first_name)) srv.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), str(message)) csvreader = csv.reader(open("contacts.csv")) for row in csvreader: first = row full = row + " " + row addresses = [row] if row != "": addresses.append(row) if row != "": addresses.append(row) a = ", ".join(map( lambda x: "%s <%s>" % (full, x), addresses)) print first, a send_m(first, a) time.sleep(1)