Script to extract names and email addresses of all recipients of email

Hi,

I have found some scripts which extract email addresses from the content of messages in Mail, but I am looking for something rather different, and wonder if such a script exists, or if someone can help me build one. I would like to create a tab delimited text file of the first name, last name and email address of all recipients of a given message in Mail. Most email addresses will be in the format John Doe johndoe@me.com.

Thanks,

Nick

Hi,

basically it’s quite easy to extract the information from a message, but the name property is only a string “ or missing value “ which can be anything, not reliably a first name [space] last name combination.

This script extracts the name and address property from each recipient of each selected message and write the result into a text file “recipients.txt” on desktop. The value “n/a” is written for a missing value name property.


tell application "Mail" to set theMessages to selection
set recipientList to {}
repeat with aMessage in theMessages
	set aList to {}
	tell application "Mail"
		set end of aList to "Message ID " & id of aMessage
		set end of aList to ""
		repeat with aRecipient in recipients of aMessage
			set recipientName to name of aRecipient
			if recipientName is missing value then set recipientName to "n/a"
			set end of aList to recipientName & tab & address of aRecipient
		end repeat
	end tell
	set end of aList to ""
	set end of aList to ""
	set end of recipientList to aList
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set resultText to recipientList as text
set text item delimiters to TID

set textFile to ((path to desktop as text) & "recipients.txt")
try
	set fileRef to open for access file textFile with write permission
	write resultText to fileRef
	close access fileRef
on error
	try
		close access file textFile
	end try
end try

Thanks very much Stefan. That works well.

I can see from the names that are included in the sample email I tested the script on that the format is not reliable!

I wonder though if the script could be adapted to check the number of spaces in the name used and then include everything after the last space as the last name, and everything before the last space as the first name? This way at least names in the format “Firstname Lastname” would be entered correctly into the text file tab delimited, and then I could manually adjust the text file for any other formats.

With Best Wishes,

Nick

Here a bash version which I use myself to get all mail addresses from a mail account based on To: and From: headers. Here’s the script.

set mailAccounts to every paragraph of (do shell script "ls $HOME/Library/Mail/V2/ | grep @")

set selectedAccounts to choose from list mailAccounts with prompt "Select your mail account(s)." with multiple selections allowed
if selectedAccounts is false then return

repeat with thisAccount in selectedAccounts
	do shell script "find $HOME/Library/Mail/V2/" & quoted form of thisAccount & " -regex '.*\\.emlx$' -exec egrep -hi '^(To|From): .*<.*@.*>' {} + | sort -fu | sed 's/ \\</;/g
	s/From: //g
	s/To: //g
	s/\"//g
	s/\\>//g 
	/^=/d' | sort -fu > $HOME/Desktop/mailContacts.csv"
end repeat

try this


tell application "Mail" to set theMessages to selection
set recipientList to {}
repeat with aMessage in theMessages
	set aList to {}
	tell application "Mail"
		set end of aList to "Message ID " & id of aMessage
		set end of aList to ""
		repeat with aRecipient in recipients of aMessage
			set recipientName to my processName(name of aRecipient)
			set end of aList to recipientName & tab & address of aRecipient
		end repeat
	end tell
	set end of aList to ""
	set end of aList to ""
	set end of recipientList to aList
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set resultText to recipientList as text
set text item delimiters to TID

set textFile to ((path to desktop as text) & "recipients.txt")
try
	set fileRef to open for access file textFile with write permission
	write resultText to fileRef
	close access fileRef
on error
	try
		close access file textFile
	end try
end try

on processName(theString)
	if theString is missing value then return "n/a"
	set {TID, text item delimiters} to {text item delimiters, space}
	set countTextItems to count text items of theString
	tell text items of theString
		if countTextItems > 2 then
			set theResult to ((items 1 thru -2) as text) & tab & last item
		else if countTextItems = 2 then
			set theResult to item 1 & tab & item 2
		else
			set theResult to theString
		end if
	end tell
	set text item delimiters to TID
	return theResult
end processName