Poor man’s mail merge in Apple Mail

Back at work after some "virtual" vacation (where virtual equals being at sea, yet nailed to a phone for most of the day… busy times over here), I have been confronted with what seemed to be a brain dead issue, that is sending a sizable number of emails to a lot of recipients. I'm not fond of Bcc: lists, so I thought there would have been an easy way to do some sort of mail merge in Apple Mail.

Unless I'm seriously missing something, that's not the case: there is no built-in functionality I could find, and all I manage to scavenge on the Net were dubious crippleware packages. There is a remote possibility using Automator, but it seems to depend from addressbook entries, while all I had was a text file with a list of email addresses.

I thought that would make for a good chance to finally have a look at AppleScript and, to my surprise, it was way easier than I thought. I'm sure there are much better ways of coding this (I particularly hate how I had to build the From: address by hand, yet apparently there is no easy way to grab that from the AppleScript dictionary), but if all you have is a text file with your e-mail in it and another file with a list of email addresses, you might find this script useful. Or not. It floats my boat, so I thought I'd share it.

Enjoy!(or just download it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
tell application "Mail" to set allAccounts to name of every account
choose from list allAccounts with title "Choose the Mail account to use..."
set theAccount to result as string
 
set subjectDialog to display dialog ¬
	"Enter the subject of the email to send" default answer "no subject"
set theSubject to text returned of subjectDialog
 
set sendOrPreview to the button returned of ¬
	(display dialog ¬
		"Send the messages right away or preview and send manually?" with title ¬
		"Send or Preview?" with icon caution ¬
		buttons {"Preview", "Send"} ¬
		default button 1)
 
set theText to (choose file with prompt "Pick a text file containing the email text")
 
set theContent to read theText
 
tell application "Finder"
	set addresses to paragraphs of ¬
		(read (choose file with prompt "Pick a text file containing email addresses, one by line"))
end tell
 
tell application "Mail"
	activate
	set activeAccount to account theAccount
	repeat with i from 1 to (the length of addresses)
		set newMessage to make new outgoing message ¬
			with properties {account:activeAccount, subject:theSubject, content:theContent}
		tell newMessage
			set sender to ¬
				((full name of activeAccount & " < " & email addresses of activeAccount as string) & ">")
			make new to recipient at end of to recipients ¬
				with properties {address:(a reference to item i of addresses)}
			set visible to true
		end tell
		if sendOrPreview is equal to "Send" then
			send newMessage
		end if
	end repeat
end tell