Apple Event: May 7th at 7 am PT

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

How to extract email addresses from message (not the sender)

I saw this post which has been archived. I need to know how to apply the script?


--


Pierre L. Québec



The following script should extract all email addresses from the body of the selected messages, and put them into the clipboard. It should then be easy to paste the contents of the clipboard into a Numbers or Excel document.


*set theAddresses to ""*
try
*set TID to AppleScript's text item delimiters*
*set AppleScript's text item delimiters to space*
*tell application "Mail"*
*repeat with theMessage in (get selection)*
*set theText to text items of (get content of theMessage)*
*repeat with thisItem in theText*
*if thisItem contains "@" then*
*set theAddresses to theAddresses & (thisItem as text) & return*
*end if*
*end repeat*
*end repeat*
*end tell*
*set the clipboard to theAddresses*
*set AppleScript's text item delimiters to TID*
*on error*
*set AppleScript's text item delimiters to TID*
*end try*
*the clipboard*


--

Posted on Aug 9, 2012 2:50 AM

Reply
Question marked as Best reply

Posted on Aug 9, 2012 7:18 AM

Paste the lines with the * into Script Editor. Remove the * from the ends of each line and click the compile button to check the syntax. Select a couple of messages in Mail then click Run in Script Editor and see what happens. If you save it in your Library/Scripts/Applications/Mail then itshould be available for running from the scipt menu when Mail is active.

39 replies

Mar 27, 2013 6:48 AM in response to Pierre L.

Thanks Pierre!


Now it´s working but i get apart of emails things like this:


<namea@gmail.com>

[mailto:nameb@gmail.com]

namea@gmail.com
Subject:


or


Tom Jones <tom.jones@yahoo.com>

tom.jones@yahoo.com


Is it possible to put in the script how to delete duplicates and sort emails please?


Is this correct?


grep -Eiorh '([[:alnum:]_.-]+@[[:alnum:]_-]+?\.[[:alpha:].]{2,6})' "$@" | sort | uniq



I don't know how to add it to your script.


Thanks again

Mar 27, 2013 10:05 AM in response to muzaa


Is this correct?


grep -Eiorh '([[:alnum:]_.-]+@[[:alnum:]_-]+?\.[[:alpha:].]{2,6})' "$@" | sort | uniq




I can't tell. I don't know much about UNIX either. Sorry!


Here's an improved version of the previous script that's more robust and removes duplicates. To sort emails, however, use Excel or Numbers or please ask someone else to help you.


Admittedly, AppleScript is not the best way to go if you have hundreds of mails to handle.



global leftDelimiters, rightDelimiters


set whiteSpaceConstants to space & tab & return & linefeed ¬

& characterid 8232 & characterid 160

set leftDelimiters to whiteSpaceConstants & "<([:;"

set rightDelimiters to whiteSpaceConstants & ">)]:;,?!"

-- modify the above delimiters if necessary


set theEmailAddresses to {} -- a list

set theRef to a reference to theEmailAddresses


tell application "Mail"

repeat with thisMessage in (get selection)

try

copy (reply to of thisMessage) to the end of theRef

repeat with thisAddress in my getAddresses(content of thisMessage)

copy contents of thisAddress to the end of theRef

end repeat

end try

end repeat

end tell


-- Remove duplicates :

set theShorterList to {}

repeat with thisAddress in theRef

set thisAddress to contents of thisAddress

if thisAddress is not in (theShorterList as text) then

copy thisAddress to the end of theShorterList

end if

end repeat


-- Convert the list to text (as a list of paragraphs) :

set TID to AppleScript's text item delimiters

set AppleScript's text item delimiters to return

set theText to (text items of theShorterList) as text

set AppleScript's text item delimiters to TID


-- Display the result in a new TextEdit document :

tell application "TextEdit"

makenewdocumentat front with properties {text:theText}

activate

end tell



on getAddresses(theText)

set theAddresses to {}

repeat

set j to offset of "@" in theText

if j = 0 then exit repeat -- no more email addresses

set thisAddress to "@"

set L to length of theText

set i to j

repeat

set i to i - 1

if i = 0 then exit repeat

if (text item i of theText) is in leftDelimiters then exit repeat

end repeat

set k to j

if i + 1 < j then

repeat

set k to k + 1

if k > L then exit repeat

if (text item k of theText) is in rightDelimiters then exit repeat

end repeat

if k - 1 > j then

set thisAddress to text (i + 1) through (k - 1) of theText

if last character of thisAddress is "." then

set thisAddress to text 1 through -2 of thisAddress

end if

copy thisAddress to the end of theAddresses

end if

end if

if (kL) or (j = L) then exit repeat

set theText to text (j + 1) through -1 of theText

end repeat

return theAddresses

end getAddresses



That's the best I can do. Hope it can help.


Message was edited by: Pierre L. (replaced “repeat with thisAddress in theEmailAddresses” with “repeat with thisAddress in theRef”

Mar 27, 2013 12:08 PM in response to muzaa

Thanks for your feedback.


I think the script could be improved a little bit further by replacing


set theShorterList to {}

repeat with thisAddress in theRef

set thisAddress to contents of thisAddress

if thisAddress is not in (theShorterList as text) then

copy thisAddress to the end of theShorterList

end if

end repeat


with


set theShorterList to {}

set theRef2 to a reference to theShorterList

repeat with thisAddress in theRef

set thisAddress to contents of thisAddress

if thisAddress is not in (theRef2 as text) then

copy thisAddress to the end of theRef2

end if

end repeat

Mar 27, 2013 12:27 PM in response to Pierre L.

It works for me same like the other one, maybe it's faster I don't know.

It's perfect. Thank you


The result of the script I have to pass through another email extractor to delete all names and brackets.


Now I get results like this for example:


adwords-noreply@google.com

ivan gimenez <iva-gime@hotmail.com>

david bravo suarez <dabrasu@hotmail.com>

info@companyfree.eu


Is there any way in the script how to get only the email address without the name please?


What do you mean with this please? It´s at the end of one of your posts:

Message was edited by: Pierre L. (replaced “repeat with thisAddress in theEmailAddresses” with “repeat with thisAddress in theRef”

Mar 27, 2013 12:52 PM in response to muzaa

Is there any way in the script how to get only the email address without the name please?


Hopefully, the following version of the script should do exactly what you are asking for (except sorting):



global leftDelimiters, rightDelimiters


set whiteSpaceConstants to space & tab & return & linefeed ¬

& characterid 8232 & characterid 160

set leftDelimiters to whiteSpaceConstants & "<([:;"

set rightDelimiters to whiteSpaceConstants & ">)]:;,?!"

-- modify the above delimiters if necessary


set theEmailAddresses to {} -- a list

set theRef to a reference to theEmailAddresses


tell application "Mail"

set N to 0

repeat with thisMessage in (get selection)

try

copy (extract address from reply to of thisMessage) to the end of theRef

repeat with thisAddress in my getAddresses(content of thisMessage)

copy contents of thisAddress to the end of theRef

end repeat

set N to N + 1

end try

end repeat

end tell


-- Remove duplicates :

set theShorterList to {}

set theRef2 to a reference to theShorterList

repeat with thisAddress in theRef

set thisAddress to contents of thisAddress

if thisAddress is not in theRef2 then

copy thisAddress to the end of theRef2

end if

end repeat


-- Convert the list to text (as a list of paragraphs) :

set TID to AppleScript's text item delimiters

set AppleScript's text item delimiters to return

set theText to (text items of theShorterList) as text

set AppleScript's text item delimiters to TID


-- Display the result in a new TextEdit document :

tell application "TextEdit"

makenewdocumentat front with properties {text:theText}

activate

end tell



on getAddresses(theText)

set theAddresses to {}

repeat

set j to offset of "@" in theText

if j = 0 then exit repeat -- no more email addresses

set thisAddress to "@"

set L to length of theText

set i to j

repeat

set i to i - 1

if i = 0 then exit repeat

if (text item i of theText) is in leftDelimiters then exit repeat

end repeat

set k to j

if i + 1 < j then

repeat

set k to k + 1

if k > L then exit repeat

if (text item k of theText) is in rightDelimiters then exit repeat

end repeat

if k - 1 > j then

set thisAddress to text (i + 1) through (k - 1) of theText

if last character of thisAddress is "." then

set thisAddress to text 1 through -2 of thisAddress

end if

copy thisAddress to the end of theAddresses

end if

end if

if (kL) or (j = L) then exit repeat

set theText to text (j + 1) through -1 of theText

end repeat

return theAddresses

end getAddresses

Jul 30, 2014 2:04 PM in response to Pierre L.

Hi Pierre,

I've gone through your script and it works, it takes out the email address found inside the body of an email.

Would you be able to help me, i receive emails from eBay with my customers information as below:

I want to extract the FULL NAME, EMAIL ADDRESS & HOME ADDRESS on a text file or csv. If you

can please help me with this, i would be ever most gratefull. Thanks



You did it. Your item has sold. Please dispatch this item to the buyer within 1 working day(s) after your buyer pays. As soon as your buyer pays, print your eBay postage label.
User uploaded file
3 reasons to print your label on eBay:
  • Get discounted rates for postage.
  • eBay label printing service is free.
  • Tracking information is uploaded automatically to My eBay and an email is sent to your buyer that their item is on the way.
If you don't use eBay label printing, upload your tracking information manually.


You should always leave Feedback for your buyer to encourage them to buy from youagain.


User uploaded file
New Skinny Mens Wedding Solid Denim Blue Plain Necktie Tie 2.5" UK Seller
End time: 07-Aug-14 19:03:14 BST
Sale price: £5.47
Quantity: 3
Quantity sold: 1
Quantity remaining: 2
Buyer: Sam Smith
sams6187 (sams@4highgreen.com) [contact buyer]


Buyer's postal address:


Sam Smith
23 Sammy Lodge
Sam Road
Bamford, Derbyshire S45 9CY United Kingdom
Sell another item | Send invoice to buyer

How to extract email addresses from message (not the sender)

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