Extract To recipients from selected e-mails, then create new messages

Hi,

Have cobbled together the following script taken from posts on this forum (thanks to the original posters). I want to select some old e-mails in Mail.app (let’s say ten), and automatically create ten new messages, one for each recipient. The recipient will be from the To field in the original e-mail, not the sender (as it was myself).

This is where I am at the moment:


tell application "Mail"
	set msgs to selection as list
	-- creating a list of found message senders
	set msgsenders to {}
	repeat with msg in msgs
		set msgsender to sender of msg
		set msgsendername to extract name from msgsender
		set msgsenderaddr to extract address from msgsender
		set msgsenders to msgsenders & {{msgsendername, msgsenderaddr}}
	end repeat
	-- creating a new message with the found senders as BCC recipients
	set newmsg to make new outgoing message with properties {visible:true}
	repeat with msgsender in msgsenders
		log msgsender
		set msgsendername to item 1 of msgsender
		set msgsenderaddr to item 2 of msgsender
		tell newmsg
			make new to recipient at end of to recipients with properties {address:msgsenderaddr, name:msgsendername}
			set subject to "Hello"
		end tell
	end repeat
end tell

I’m stuck with making the recipient the To recipient, not sender, and making a new message with each.

Many thanks if you can help,

Osmo

Hi,

try this


tell application "Mail"
	set msgs to selection
	-- creating a list of found message senders
	
	repeat with msg in msgs
		tell (sender of msg) to set {msgsendername, msgsenderaddr} to {extract name from, extract address from}
		set newmsg to make new outgoing message with properties {visible:true}
		tell newmsg
			make new to recipient at end of to recipients with properties {address:msgsenderaddr, name:msgsendername}
			set subject to "Hello"
		end tell
	end repeat
end tell


Many thanks for your help, Stefan.

That script looks so much more simple. It’s still using the FROM address though, not the TO address.

For example, if I send an e-mail from ME.com to YOU.com, I want the script to make a new message with the recipient as YOU.com. It would be as though I’m re-sending the message.

Is there anyway to do that?

Hi,
Is it something like this you are after? It extracts all recipients from the selected mails, makes one new mail and adds the extracted recipients to the new mail.

tell application "Mail"
	-- get recipients of selection
	set allRecipients to extractRecipients of me from selection
	
	-- make new outgoing message
	set newMsg to make new outgoing message with properties {visible:true, subject:"Hello World"}
	tell newMsg
		-- add the recipients to the message
		set processedAddresses to {} -- (making sure that there are no doubles)
		repeat with i in allRecipients
			set recAddress to address of i
			set recName to name of i
			if processedAddresses does not contain recAddress then
				make new to recipient at end of to recipients with properties {address:recAddress, name:name} -- (make new recip)
				set end of processedAddresses to recAddress -- (add to check list: no doubles)
			end if
		end repeat
	end tell
end tell

on extractRecipients from mails
	set recip to {}
	tell application "Mail"
		repeat with i in mails
			repeat with z in (every recipient of i)
				set end of recip to z
			end repeat
		end repeat
	end tell
	
	return recip
end extractRecipients

Hope it helps,
ief2

Hi, ief2, thanks for trying to help as well. I want it to do as Stefan’s script does, creating new messages, but with your script’s ability to use the To recipients.

OK, here we go


tell application "Mail"
	set msgs to selection
	-- creating a list of found message senders
	
	repeat with msg in msgs
		tell (to recipient 1 of msg) to set {msgRecipientName, msgRecipientAddr} to {its name, its address}
		set newmsg to make new outgoing message with properties {visible:true}
		tell newmsg
			make new to recipient at end of to recipients with properties {address:msgRecipientAddr, name:msgRecipientName}
			set subject to "Hello"
		end tell
	end repeat
end tell

Hi, Stefan,

Comes back with The variable msgRecipientName is not defined.


tell application "Mail"
	set msgs to selection
	-- creating a list of found message senders
	
	repeat with msg in msgs
		tell (to recipient 1 of msg) to set {msgRecipientName, msgRecipientAddr} to {its name, its address}
		try
			msgRecipientName
		on error
			set msgRecipientName to ""
		end try
		set newmsg to make new outgoing message with properties {visible:true}
		tell newmsg
			make new to recipient at end of to recipients with properties {address:msgRecipientAddr, name:msgRecipientName}
			set subject to "Hello"
		end tell
	end repeat
end tell


BINGO!!! :smiley:

Before you updated this final version I got it working by removing the msgRecipientName parts, but your updated script still has them in and works like a charm.

Thanks again for your expert help. I can start bombarding potential clients with my updated CV (resumé).

Wonder if you can quickly help me sort this final issue out. I’m adding an attachment to each message but thanks to Apple, there’s a bug that puts a black background on the message. The workaround is to change the message from rich text to plain, and back again. There’s something in the script that is stopping the keystrokes from taking effect. I suspect it’s the repeat function but don’t have the knowledge to fix it.


tell application "Mail"
	
	set msgs to selection
	-- creating a list of found message senders
	
	repeat with msg in msgs
		tell (to recipient 1 of msg) to set {msgRecipientName, msgRecipientAddr} to {its name, its address}
		try
			msgRecipientName
		on error
			set msgRecipientName to ""
		end try
		set newmsg to make new outgoing message with properties {visible:true}
		tell newmsg
			make new to recipient at end of to recipients with properties {address:msgRecipientAddr, name:msgRecipientName}
			set subject to "Hello"
			
			set content to "Dear ,
"
			make new attachment with properties {file name:"file path here" as alias} at after last paragraph
			tell application "Mail" to activate
			tell application "System Events" to tell process "Mail"
				if name of menu item -1 of menu 1 of menu bar item 8 of menu bar 1 contains "Plain Text" then
					keystroke "t" using {shift down, command down}
				end if
			end tell
			
		end tell
		
		
	end repeat
	
end tell



I don’t like these nested application tell blocks
This works on my machine (10.5.8 PPC)


tell application "Mail"
	activate
	set msgs to selection
end tell
-- creating a list of found message senders

repeat with msg in msgs
	tell application "Mail"
		tell (to recipient 1 of msg) to set {msgRecipientName, msgRecipientAddr} to {its name, its address}
		try
			msgRecipientName
		on error
			set msgRecipientName to ""
		end try
		set newmsg to make new outgoing message with properties {visible:true}
		tell newmsg
			make new to recipient at end of to recipients with properties {address:msgRecipientAddr, name:msgRecipientName}
			set subject to "Hello"
			
			set content to "Dear ,
"
			make new attachment with properties {file name:alias "path:to:file.ext"} at after last paragraph
		end tell
		tell application "System Events" to tell process "Mail"
			if name of menu item -1 of menu 1 of menu bar item 8 of menu bar 1 contains "Plain Text" then
				keystroke "t" using {shift down, command down}
			end if
		end tell
	end tell
end repeat


Didn’t work at first but added delay 1 and now works perfectly. Thanks again, you’re a scholar and a gent.

Hello.

Safari was updated today :slight_smile: and should remove the black messages bug from mail.