Serial Mailer

While I was writing my bachelor thesis, I at one point had to reach lots and lots of people to send them a questionnaire. I got all of their mail addresses from an official directory, threw them in a plain text file (one address per line) and was then thinking about how to properly work this out. All in all, there were more than 3000 addresses, that had to be reached.

To simply throw all recipients into the address field of the mail was a no-go from the start. That was simply not going to work, privacy-wise. Even though the mail addresses were freely available on the internet without much effort, the recipients shouldn’t see who else got the mail. There have already been enough disasters with the “reply all”-Button.

The second possibility was to move all recipients to the BCC-field. But then no recipient is shown, which would have led any sane mail program to believe that the mail was spam, since we had no interactions before. So that was not an option either. A possible workaround was to only put one person in the address field and send the rest a blind carbon copy. But I decided against it, because that would have looked strange somehow, too.

So I had to send every recipient their own, separate mail somehow. There are a couple of apps in the Mac AppStore (like SerialMailer or AutoMailer). But about 20€ seemed to be too much for such a one-time mission. Besides, the graphical features would have been too much for my needs, all I needed was a plain and simple mail asking for assistance and providing a link to the questionnaire.

Luckily, Mac OS X provides useful tools like Automator and AppleScript. My first attempt at sending mails with Automator, which explicitly lists serial mails as a feature, failed with a pretty complicated error:

-[__NSArrayM numberOfItems]: unrecognized selector sent to instance

Since I couldn’t find much about that on the interwebs, I decided to go for AppleScript and learn that language to the point that I could send my roundabout 3000 mails with it.

Since I knew some basic programming back then, I didn’t have much trouble learning the language, since it is a language very close to the “real English” language. Additionally, I was copying bits and pieces from all around the internet, when I got stuck on something. As long as I fully understood what they were doing, anyway, I didn’t want to lose control over a program that would send such a huge amount of mails. I was really surprised, that there were a lot of solutions for serial mails with attachments and addresses from the contact list, but nothing for my quite simple problem.

For that reason, I want to share my script on this page, in case anyone happens to have the same problem as I had.

The script needs a plain text file with the mail addresses (one per line) and on plain text file with the mail body/content. You should also be careful about having too much addresses at once, at about 1200 mails, the script stopped responding for me. Either the mail.app or the script itself is overwhelmed by the amount, causing your computer to slow down and behave unresponsive. The best way to go is to separate the address files into neat portions of about 800 addresses. And thanks to «class utf8», the script can handle umlauts like ä, ö and ü (and probably other, language specific ones).

Have fun and good luck!

-- SerialMail
-- simple plain text serial mailer for AppleScript

set MailSubject to "[Your mail subject goes here]"

-- List of all recipients
tell application "Finder"
	set emailList to {}
	set Adresses to paragraphs of (read (choose file with prompt "Choose the text file with the mail addresses") as «class utf8»)
	repeat with nextLine in Adresses
		if length of nextLine is greater than 0 then
			set end of emailList to nextLine
		end if
	end repeat
end tell

set count1 to count of emailList

-- Mail text
tell application "Finder"
	set emailText to (read (choose file with prompt "Choose the text file containing the mail content") as «class utf8»)
end tell

-- Create serial mails and send them
tell application "Mail"
	activate
	repeat with i from 1 to count1
		set theAdress to (item i of emailList)
		set theMex to (make new outgoing message at end of outgoing messages with properties {visible:true, subject:MailSubject, content:emailText})
		tell theMex
			make new to recipient at end of to recipients with properties {address:theAdress}
		end tell
		send theMex
	end repeat
end tell

display dialog (count1 as string) & " messages sent."

If you have any ideas for improvement, comments or whatever, you can find this script on github, too.

Leave a Reply

Your email address will not be published. Required fields are marked *