1 2 Previous Next 28 Replies Latest reply: Jun 10, 2013 8:16 AM by lfaoro
muzaa Level 1 Level 1 (0 points)

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)
  • 1. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    I have tried a few things and think that you could start with:

     

    grep -r From: /Users/neville/Library/Mail | cut -f3 -d:

     

    Change my name to yours - paste to a Terminal window and hit return.

     

    Clearly further work needs doing.

     

    If necessary it could be put into an AppleScript wrapper.

  • 2. Re: Extract email addresses from email header - Sender (From) - Mail
    muzaa Level 1 Level 1 (0 points)

    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.

  • 3. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    It should have recursively searched all files in your folder at /Users/user-name/Library/Mail

     

    Have a look in the folder and examine the structure to see how to alter the command to point to the account you want.

  • 4. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    Here is an updated version with duplicate addresses removed:

     

    grep -rh From: /Users/neville/Library/Mail | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' 
    
    
  • 5. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    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.

  • 6. Re: Extract email addresses from email header - Sender (From) - Mail
    muzaa Level 1 Level 1 (0 points)

    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!

  • 7. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    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:

     

    Picture 2.png

     

    Picture 1.png

     

    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'.

  • 8. Re: Extract email addresses from email header - Sender (From) - Mail
    softwater Level 5 Level 5 (5,370 points)

    Didn't you get the answer here?

  • 9. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)
    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.
     
    
  • 10. Re: Extract email addresses from email header - Sender (From) - Mail
    muzaa Level 1 Level 1 (0 points)

    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.

  • 11. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    Two things to check for:

     

    1 - the input path should point to one of your accounts and not mine

     

    2 - existence of first and last characters after copy/paste - the final double quote is missing above.

  • 12. Re: Extract email addresses from email header - Sender (From) - Mail
    muzaa Level 1 Level 1 (0 points)

    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\"`"

  • 13. Re: Extract email addresses from email header - Sender (From) - Mail
    Neville Hillyer Level 4 Level 4 (1,855 points)

    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.

  • 14. Re: Extract email addresses from email header - Sender (From) - Mail
    muzaa Level 1 Level 1 (0 points)

    Neville it's ok in Applescript!

     

    I write in AppleScript:

     

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

     

    Then I run it and get result: ""

1 2 Previous Next