Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Extracting email headers from Apple Mail

Hello friends...


I have hundreds of email received / sent to various people whose accounts are in the same email server, for example @xyz.gov.in

( sony@xyz.gov.in , tony@xyz.gov.in etc.)


I need to search and extract the following header information from all email sent to / received from email server, for example, @xyz.gov.in, in the following format:


From:

To:

Cc:

Bcc:

Date & Time:

Subject:


Is there any script that can do this for me and create a text or .csv or any other file?


All messages are stored in Apple mail, imap, pop and offline folders.


My OS: Mac OS 10.8.3

Apple Mail Version: 6.3


many thanks in advance


Amit

MacBook Pro (15-inch 2.4/2.2 GHz), OS X Mountain Lion (10.8.3)

Posted on Apr 28, 2013 6:58 AM

Reply
39 replies

Apr 29, 2013 2:36 AM in response to auromira

My difficulty is that I only have an old Mac with Leopard and it appears that AppleScript which works on Leopard does not work on your Mac.


I will leave you in the capable hands of pjdube.


Perhaps you should give some thought to your desired output. I was aiming for a comma separated file without any header names (ie 'From', 'To' etc) but your latest message to me made me doubt that this is what you want. Much may depend upon how the file is going to be used. Input to Excel or a database? Multiple addresses in a single field could be separated by eg ':' or something else. Since many characters can appear in Subjects and even email addresses some method of coping with such occurrences needs to be found. A tab separated list may be safer.

Apr 29, 2013 9:03 PM in response to auromira

try this ugly mess of code (sorry there aren't many comments, bit busy this evening. but it worked over a couple of quick, simple tests):


set outputList to {}

set phrase to "@sebi.gov.in"

set saveFilePath to "/Users/<yourname>/Desktop/tab-del file.txt" --set this to correct save path


--drill down to message level and test for phrase

tell application "Mail"

set myAccounts to every account

repeat with thisAccount in myAccounts

tell thisAccount

set myMailboxes to every mailbox

repeat with thisMailbox in myMailboxes

tell thisMailbox

set myMesssages to every message

repeat with thisMessage in myMesssages

set messageDataList to {}

tell thisMessage

set senderAddress to extract address fromsender

if senderAddress ends with phrase then


-- check sender

set end of outputList to (my tabFormatString(thisMessage))

else

set recipientAddresses to (recipients's address)

repeat with thisaddress in recipientAddresses


-- check each recipient

if thisaddress ends with phrase then

set end of outputList to (my tabFormatString(thisMessage))

exit repeat

end if

end repeat

end if

end tell

end repeat

end tell

end repeat

end tell

end repeat

end tell


set fp to open for accesssaveFilePath with write permission

writetid({input:outputList, delim:return}) tofp

close accessfp


on tabFormatString(aMessage)


--build a tab delimited string from the relevant data

tell application "Mail"

tell aMessage

set messageFrom to sender

set messageToNameList to to recipients's name

set messageToAddressList to to recipients's address

set messageCCNameList to cc recipients's name

set messageCCAddressList to cc recipients's address

set messageBCCNameList to bcc recipients's name

set messageBCCAddressList to bcc recipients's address

set messageDate to (date received) as rich text

set messageSubject to subject

end tell

end tell


set txt to {messageFrom}

set end of txt to mungeParallelLists(messageToNameList, messageToAddressList)

set end of txt to mungeParallelLists(messageCCNameList, messageCCAddressList)

set end of txt to mungeParallelLists(messageBCCNameList, messageBCCAddressList)

set end of txt to messageDate

set end of txt to messageSubject


return tid({input:txt, delim:tab})

end tabFormatString


on mungeParallelLists(list1, list2)


-- utility to turn lists like {{name1, name2,…},{address1, address2, …}}


-- into "name1 <address1>, name2 <address2> ...

set tempList to {}

repeat with i from 1 to count of list1

set end of tempList to ((item i of list1 & " <" & item i of list2 & ">") as text)

end repeat

return tid({input:tempList, delim:", "})

end mungeParallelLists


on tid({input:input, delim:delim})


-- handler for text items


set {oldTID, my text item delimiters} to {my text item delimiters, delim}

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid

Apr 29, 2013 9:19 PM in response to twtwtw

I pasted the entire script in the Apple Script Editor and ran it.


It resulted in following error.


error "item 3 of {«class mbxp» \"Junk\" of «class mact» \"iCloud\" of application \"Mail\", «class mbxp» \"Archive\" of «class mact» \"iCloud\" of application \"Mail\", «class mbxp» \"Notes\" of «class mact» \"iCloud\" of application \"Mail\", «class mbxp» \"INBOX\" of «class mact» \"iCloud\" of application \"Mail\", «class mbxp» \"Drafts\" of «class mact» \"iCloud\" of application \"Mail\", «class mbxp» \"Sent Messages\" of «class mact» \"iCloud\" of application \"Mail\", «class mbxp» \"Deleted Messages\" of «class mact» \"iCloud\" of application \"Mail\"} doesn’t understand the count message." number -1708 from item 3 ofclass mbxp» "Junk" of «class mact» "iCloud", «class mbxp» "Archive" of «class mact» "iCloud", «class mbxp» "Notes" of «class mact» "iCloud", «class mbxp» "INBOX" of «class mact» "iCloud", «class mbxp» "Drafts" of «class mact» "iCloud", «class mbxp» "Sent Messages" of «class mact» "iCloud", «class mbxp» "Deleted Messages" of «class mact» "iCloud"}

Apr 29, 2013 9:25 PM in response to auromira

substitute this code in. it should exclude iCloud and Junk mail, which I assume you don't care about anyway.


--drill down to message level and test for phrase

tell application "Mail"

set myAccounts to (every account whose name is not "iCloud")

repeat with thisAccount in myAccounts

tell thisAccount

set myMailboxes to (every mailbox whose name is not "Junk")

Apr 30, 2013 11:05 AM in response to auromira

Grrr... annoying applescript problems. alright, next thing to try. set up a smart mailbox with the following parameters:


User uploaded file


select that mailbox, then run the following script, which is basically the same script as before except it gets its items from the currently open mailbox of the frontmost viewer.


set outputList to {}

set saveFilePath to "/Users/<yourname>/Desktop/tab-del file.txt"


tell application "Mail"

set sebiMessages to every message of first message viewer

repeat with i from 1 to count of sebiMessages

set end of outputList to (my tabFormatString(itemi of sebiMessages))

if i mod 100 = 0 then beep

end repeat

end tell


set fp to open for accesssaveFilePath with write permission

writetid({input:outputList, delim:return}) tofp

close accessfp


on tabFormatString(aMessage)


--build a tab delimited string from the relevent data

tell application "Mail"

tell aMessage

set messageFrom to sender

set messageToNameList to to recipients's name

set messageToAddressList to to recipients's address

set messageCCNameList to cc recipients's name

set messageCCAddressList to cc recipients's address

set messageBCCNameList to bcc recipients's name

set messageBCCAddressList to bcc recipients's address

set messageDate to (date received) as rich text

set messageSubject to subject

end tell

end tell


set txt to {messageFrom}

set end of txt to mungeParallelLists(messageToNameList, messageToAddressList)

set end of txt to mungeParallelLists(messageCCNameList, messageCCAddressList)

set end of txt to mungeParallelLists(messageBCCNameList, messageBCCAddressList)

set end of txt to messageDate

set end of txt to messageSubject


return tid({input:txt, delim:tab})

end tabFormatString


on mungeParallelLists(list1, list2)


-- utility to turn lists like {{name1, name2,…},{address1, address2, …}}


-- into "name1 <address1>, name2 <address2> ...

set tempList to {}

repeat with i from 1 to count of list1

set mungeName to item i of list1

set mungeAddress to itemi of list2

if mungeName is missing value then set mungeName to ""

set end of tempList to ((mungeName & " <" & mungeAddress & ">") as text)

end repeat

return tid({input:tempList, delim:", "})

end mungeParallelLists


on tid({input:input, delim:delim})


-- handler for text items


set {oldTID, my text item delimiters} to {my text item delimiters, delim}

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid

Extracting email headers from Apple Mail

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.