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 28, 2013 9:53 AM in response to Neville Hillyer

In other words, this is what I am looking for...


Option 1


If there is a script that extracts all the headers of all mails stored in apple mail (offline, pop, imap), to a .csv file. This would require filtering in MS Excel or something for final output.


Option 2


If there is a script, that is capable of custom search emails mails stored in apple mail (offline, pop, imap) and giving an output for each email in the following format, nothing like it...


From:

To:

Cc:

Bcc:

Date & Time:

Subject:



best


Amit

Apr 28, 2013 1:23 PM in response to auromira

Below is an initial test script you can try.


Put a few messages in a mailbox entitled 'test'.


Paste the script into Script Editor and run it.


You should see the result in a file on your Desktop called headers.txt


My main concern is what to do about multiple addresses in To, Cc and Bcc - perhaps you could think about the format you would like for these.


I have yet to find a way to cope with no Cc addresses etc but this may resolve itself when I start looping through them.


tell application "Mail" repeat with lastMsg from (count messages of mailbox "test") to 1 by -1 try set thisMsg to message lastMsg of mailbox "test" end try set f to sender of thisMsg set ff to quoted form of f set t to address of item 1 of to recipients of thisMsg set c to address of item 1 of cc recipients of thisMsg -- set cc to quoted form of c set b to address of item 1 of bcc recipients of thisMsg -- set bb to quoted form of c set d to date sent of thisMsg set s to subject of thisMsg do shell script "echo " & ff & ", " & t & ", " & d & ", " & s & " >> ~/Desktop/headers.txt" end repeat end tell

Apr 28, 2013 8:43 PM in response to Neville Hillyer

Dear Neville,


1.


I created a mailbox "test" and put in some 265 messages in it.


I copied the script in Apple Script Editor and ran it.


It returned the following error:


tell application "Mail"

count every message of mailbox "test"


--> error number -1728 from mailbox "test"

Result:

error "Mail got an error: Can’t get mailbox \"test\"." number -1728 from mailbox "test"



2.


a. If the output is in a .csv file, and requires filtering, then the mutiple addressess in from/to/cc/bcc feilds have to be in separate feilds.


b. However, if the script can search all mailboxes for mails with headers that contain "@sebi.gov.in" in from/to/cc/bcc feilds and extracts header information from only those emails, and give an output for each of such emails in the following format, then multiple email addresses in the same feild obviously do not matter.


From:

To:

Cc:

Bcc:

Date & Time:

Subject:


If you wish to skype, my handle is: amitpix


This exercise that I need to do is in larger public interest to bell an allegedly Invester Hostile Market Regulator 😉. Your help in this regard will be highly appreciated.


best


Amit

Apr 28, 2013 8:57 PM in response to auromira

This should work:


tell application "Mail"

set selectionMessage to selection-- just select the first message in the folder

set thisMessage to item 1 of selectionMessage

set theseMessages to (every message in (mailbox of thisMessage))

set listOfEmails to {}



-- End of Original set and beginning of new set


repeat with eachMessage in theseMessages

set theFrom to (extract address fromsender of eachMessage)

if listOfEmails does not contain theFrom then

set the end of listOfEmails to theFrom

end if



-- To field Extract


set toAddresses to address of to recipients of eachMessage

repeat with i from 1 to (counttoAddresses) -- The repeat won't happen if toAddresses is empty

set theTo to itemi of toAddresses

if listOfEmails does not contain theTo then

set the end of listOfEmails to theTo

end if


end repeat



-- BCC Extract


set bccAddresses to address of bcc recipients of eachMessage

repeat with i from 1 to (count bccAddresses)

set thebcc to itemi of bccAddresses

if listOfEmails does not contain thebcc then

set the end of listOfEmails to thebcc

end if


end repeat



-- CC Extract


set ccAddresses to address of cc recipients of eachMessage

repeat with i from 1 to (count ccAddresses)

set theCC to itemi of ccAddresses

if listOfEmails does not contain theCC then

set the end of listOfEmails to theCC

end if


end repeat


end repeat

end tell


set SortedListOfEmails to simple_sort(listOfEmails)

set ptd to path todocuments folderasstring

set theFile to ptd & "extracted.txt"

set theFileID to open for accesstheFile with write permission

try

repeat with i from 1 to count of SortedListOfEmails


writeitemi of SortedListOfEmails & returntotheFileIDas «class utf8»

end repeat

end try

close accesstheFileID


on simple_sort(my_list)

set the index_list to {}

set the sorted_list to {}

repeat (the number of items in my_list) times

set the low_item to ""

repeat with i from 1 to (number of items in my_list)

if i is not in the index_list then

set this_item to item i of my_list as text

if the low_item is "" then

set the low_item to this_item

set the low_item_index to i

else if this_item comes before the low_item then

set the low_item to this_item

set the low_item_index to i

end if

end if

end repeat

set the end of sorted_list to the low_item

set the end of the index_list to the low_item_index

end repeat

return the sorted_list

end simple_sort


You should be able to start from that.

Apr 28, 2013 9:00 PM in response to auromira

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:



Once you get the script to work, I would actually make a Mail Rule to filter out the @xyg.gov.in and put all of it in a Smart Mailbox, and then run the above script, adjusting it to your needs.

Apr 28, 2013 9:22 PM in response to pjdube

Dear Mr. Dube,


I am not a software person unfortunately and know nothing much about apple scripts.


All that I need to do is the following:


Extract header information from all emails sent / received from various email addresses ending with "@sebi.gov.in", in the following format for each of such emails.



From:

To:

Cc:

Bcc:

Date & Time:

Subject:


These emails all stored in various folders of Apple Mail v6.3. My Mac OS is 10.8.3


I would try the smart mailbox but not sure if it filters out accurately.


many thanks in advance.

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.