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

Extract email addresses from email header - Sender (From) - Mail

Hi!

I would like to extract email addresses from email header from field "Sender (From)" and from email body in Mail at the same time.

I saw only scripts how to extract from email body.


I get emails asking for some informations about product and many people don't include their email address in the body, so I have to extract from email header as well.


I would like to extract email addresses from whole email account, separate alphabeticaly, delete duplicates and save it (separated by comma) in text file.

This I would use for sending massive email to all customers.

I'm on OSX 10.6.8.

Do you have please somebody script for extracting email addresses in the way:

1. select mail account

2. run script

3. save email addresses to txt file


Thank you for help and advise!

MacBook Pro, Mac OS X (10.6.8)

Posted on Mar 24, 2013 5:25 AM

Reply
29 replies

Mar 24, 2013 6:18 PM in response to Neville Hillyer

Thank you Neville!


I tried what you said, but it doesn't work for me. I just get some email addresses from one account.


I was thinking of some script for automator.


I need to extract email addresses FROM SELECTED email account and save the result in .txt file.

I have many email accounts and I need to select one account I want to extract from.

Mar 25, 2013 5:02 PM in response to muzaa

Here is a version with output to a time stamped file on the Desktop:


i=~/Library/Mail/n.hillyer@pop.gmail.com # Input file path o=~/Desktop/ # Output file path n=`date "+%y%m%d%H%M%S"`-"addresses" # time stamped file name grep -rh From: $i | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > $o$n


Don't forget to change the input file path.

Mar 25, 2013 7:21 PM in response to Neville Hillyer

Hi and thank you again!


It's nearly what I need Neville 🙂


I change the file path. Then I ran it in terminal.


It can extract to .txt file only email addresses from email header which includes after the name as well email address.


Example:

From: Neville Hillyer <n.hillyer@gmail.com>


If there is in header only the name without the email address then it's not extracted (email is visible after secondary click on the name)


Example:

From: Neville Hillyer


I just checked that most of emails I have has in the emailheader only the name without the email address so I miss most of email addresses.


Do you have any idea what to do with it please?

Thank you in advance for your time and effort!

Mar 26, 2013 3:48 AM in response to muzaa

If there is in header only the name without the email address then it's not extracted (email is visible after secondary click on the name)


This is a Mail setting - see below:


User uploaded file


User uploaded file


Removing this option should correct this for you. It is interesting that Mail edits the source code. This will cause difficulties for those who export directly from these files without removing the option first.


The script only extracts 'From' addresses it ignores other headers such as 'Reply to'.

Mar 26, 2013 5:34 AM in response to muzaa

Here it is in an AppleScript wrapper: do shell script "grep -rh From: ~/Library/Mail/n.hillyer@pop.gmail.com | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > ~/Desktop/`date \"+%y%m%d%H%M%S\"`" As before edit ~/Library/Mail/n.hillyer@pop.gmail.com as required. The output file is defined at the end. Let me know if you want this changed.

Mar 26, 2013 5:54 PM in response to Neville Hillyer

Hi Neville!


1.

I switched off in Mail "Use Smart Addresses".

I changed the path to required mail account and then I ran in terminal:


grep -rh From: ~/Library/Mail/n.hillyer@pop.gmail.com | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > ~/Desktop/`date \"+%y%m%d%H%M%S\"`


After I got this error:


date: illegal time format

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...

[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

date: illegal time format

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...

[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

-bash: /Users/muzaa/Desktop/: Is a directory


2. Then I tried to run again the first code after switching of "Use Smart Addresses" but I get same result. Even if there is in the header now visible the email address.

Mar 27, 2013 5:12 AM in response to Neville Hillyer

Hi Neville!

Last time I was writing basic programs in Turbopascal in 1996.


Maybe I'm doing something wrong...


1. I switched off in Mail "Use Smart Addresses".

2. I changed the path to my account in both commands:


Command A.

i=~/Library/Mail/muzaa@gmail.com@pop.gmail.com # Input file path

o=~/Desktop/ # Output file path

n=`date "+%y%m%d%H%M%S"`-"addresses" # time stamped file name

grep -rh From: $i | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > $o$n



Command B.

grep -rh From: ~/Library/Mail/muzaa@gmail.com@pop.gmail.com | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > ~/Desktop/`date \"+%y%m%d%H%M%S\"`"


3. I write commands in TERMINAL.


result of Command A:

List of couple emails same like before switching of "Use Smart Addresses"


I answered it already above:

It can extract to .txt file only email addresses from email header which includes after the name as well email address.


Example:

From: Neville Hillyer <n.hillyer@gmail.com>


If there is in header only the name without the email address then it's not extracted (email is visible after secondary click on the name)


Example:

From: Neville Hillyer


I just checked that most of emails I have has in the emailheader only the name without the email address so I miss most of email addresses.


Result of Command B:


>

And nothing going on...



4. I tried the same in AppleScript editor


Result of Command A:


Syntax error. A unknown token can’t go after this identifier.


I changed the path but I still get syntax errors

(instead of i=~/Library... i put i= Users/muzaa/Library...)


Result of Command B:


Syntax error: A “from” can’t go after this identifier.


grep -rh From: /Users/radimmuzikant/Library/Mail/POP-almeacz@gmail.com@pop.gmail.com | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > ~/Desktop/`date \"+%y%m%d%H%M%S\"`"

Mar 27, 2013 8:12 AM in response to muzaa

My latest code was AppleScript to be pasted into Script Editor. It adds:


do shell script "


to the beginning


and


"


to the end


of Terminal commands.


AppleScript also needs double quotes escaped with


\


which would have to be removed for Terminal use.


Would you prefer me to convert my latest script for Terminal use?


All my tests indicate that removing Smart Addresses corrects the name only issue.

Extract email addresses from email header - Sender (From) - Mail

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