Send multiple emails changing address fro each

Ok, here goes! I need to send multiple emails, one at a time, with the address changing to a new address for each email.
we are sending out an invitation to a select group of people and we do not want to miss anyone, so we do not offend anyone by omission.
The reason we want to send individual emails is to get around group mailing filters.
I have the email address’s in filemaker so I can produce any list needed.

So far I can produce and send the email I need, But I am new to scripting so I need help with getting the Address’s from the list, and after sending, repeat to the beginning and the next on the list.

Here’s what I have so far.


set theAddress to "next on the list"

-- What is the subject of the Emails
set theSubject to "email title"

-- 3 choices,Type, copy/paste, or set full path to the document
set theBody to "Hello!

Thank you,"

-- add atachment. you need to set the full path, in this example the file is on the Desktop
set theAttachment to alias "Macintosh HD:Users:username:Desktop:name of attachment"

-- Choose the account to send the message from
set theSender to "email@company"

tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
	tell newMessage
		-- Default is false. Determines whether the compose window will
		-- show on the screen or whether it will happen in the background.
		set visible to true -- I will set to false when complete
		set sender to theSender
		make new to recipient at end of to recipients with properties {address:theAddress}
		tell content
			-- Position must be specified for attachments
			make new attachment with properties {file name:theAttachment} at after the last paragraph
			tell application "Mail"
                          -- newMessage - removed from after send, so it will not, if you want to try this script
				send 
			end tell
		end tell
	end tell
end tell

Parts fo this script are a descended from Apple sample code that I altered to suit my needs.

Thanks for your help!
Joe

A simpl way would be to have a plain text file with an addresses in one per line.
Exampl…

firstmail@themail.com
secondAddress@d.com
mail@themail.com
Address@d.com

Applescript sees each new line as a paragraph. So this Script counts the paragraphs and uses a repeat loop to go through the list.


--plain text file with addresses
set theAddressDOC to read file ("Macintosh HD:Users:username:Desktop:Address.txt")
set pcount to count paragraphs in theAddressDOC
repeat with i from 1 to number of paragraphs in theAddressDOC
	set this_item to paragraph i of theAddressDOC

	set theAddress to this_item
	
	-- What is the subject of the Emails
	set theSubject to "email title"
	
	-- 3 choices,Type, copy/paste, or set full path to the document
	set theBody to "Hello!

Thank you,"
	
	-- add atachment. you need to set the full path, in this example the file is on the Desktop
	set theAttachment to alias "Macintosh HD:Users:username:Desktop:name of attachment"
	
	-- Choose the account to send the message from
	set theSender to "email@company"
	
	tell application "Mail"
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		tell newMessage
			-- Default is false. Determines whether the compose window will
			-- show on the screen or whether it will happen in the background.
			set visible to true -- I will set to false when complete
			set sender to theSender
			make new to recipient at end of to recipients with properties {address:theAddress}
			tell content
				-- Position must be specified for attachments
				make new attachment with properties {file name:theAttachment} at after the last paragraph
				tell application "Mail"
					-- newMessage - removed from after send, so it will not, if you want to try this script
					send
				end tell
			end tell
		end tell
	end tell
	
end repeat

Thanks Mark,

That works great and will work fine for what we are doing.

I do have another question.

lets take the same situation , but I want to add the address and a name to the top of the body from a two collum page (tab delimited, Excel,or other) which is better and why?
Now to throw in a twist I do not have a name for every entry, so for those how about a default title like “To whom it may concern”

Address Name
info@abc.com Jane Doe
test@123.com Mike Send
script@com.com
try@test.com Can Doo

I do not need these for what we are doing, but I thought of these questions and felt the script would not be complete with out the answers.

Thanks for the help!



--plain text file with addresses an tab delimited
set theAddressDOC to read file ("Macintosh HD:Users:username:Desktop:Address.txt")

set pcount to count paragraphs in theAddressDOC
repeat with i from 1 to number of paragraphs in theAddressDOC
	set this_item to paragraph i of theAddressDOC
	set Name_text to "" -- set to blank
	-- using try so if there is a an error, in this case it will be when the is no name, the script will carry on
	-- instead of stopping. The Name_text will reamin as "" if it does
	try
		--use delimiters (tab) in this case to split the result of  this_item and try to set Name_text to the name
		set AppleScript's text item delimiters to tab
		set para_text to text of this_item
		--get the second half of paragraph 
		set Name_text to text item 2 of para_text
	end try
	--get the first half of paragraph 
	set address_text to text item 1 of para_text
	
	--reset the delimiters
	set AppleScript's text item delimiters to ""
	
	set theAddress to address_text
	
	-- What is the subject of the Emails
	set theSubject to "email title"
	
	-- Check to see if  Name_text contains a name or not
	if Name_text is "" then
		--if no name 
		set theBody to "To whom it may concern,
	

Thank you"
		
	else
		--the must be a name 
		set theBody to "Hello! " & Name_text & ",
	

Thank you"
	end if
	theBody
	-- add atachment. you need to set the full path, in this example the file is on the Desktop
	set theAttachment to alias "Macintosh HD:Users:username:Desktop:name of attachment"
	
	-- Choose the account to send the message from
	
	set theSender to "email@company"
	tell application "Mail"
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		tell newMessage
			-- Default is false. Determines whether the compose window will
			-- show on the screen or whether it will happen in the background.
			set visible to true -- I will set to false when complete
			set sender to theSender
			make new to recipient at end of to recipients with properties {address:theAddress}
			tell content
				-- Position must be specified for attachments
				make new attachment with properties {file name:theAttachment} at after the last paragraph
				tell application "Mail"
					-- newMessage - removed from after send, so it will not, if you want to try this script
					send
				end tell
			end tell
		end tell
	end tell
	
	
end repeat


Mark; AppleScript understands the word tab (as it does return) in a string. For clarity’s sake that’s much better than inserting the character in quotes.

Doh, Thanks adam, I knew that, :rolleyes:
I did it that way as I had just been working on a script which needed to read tabs but the only way to get the script to read the tab was paste it. (do shell"… , which explains that problem) and had no coffee to hand… :slight_smile:

Have amended the above script. Also noticed an error which I fixed.

Thank You Mark & Adam,

In my trial the script is working great!

I just added to it to direct to a letter for the text of the email and I am going to add a delay
to allow the time for mail to process the email and send it so as not to bog down the email server.

I plan on running a test of 50 emails monday, Then I will post it along with the time it took for the trial.

Thanks Again!