I’ve been getting so much spam recently that I have decided to switch to an “opt-in” approach to receiving emails. I have always set up a new email address whenever I register for something – e.g. companyname@mydomain.com – and previously, these would all arrive into a catch-all address on my email server which catches [anything]@mydomain.com.

Applescript

Just recently, this has led to a huge amount of junk hitting this catch-all address. So I had a need to track down all of the companyname@mydomain.com addresses I have ever used. Unfortunately, Entourage 2004 doesn’t make this very easy – it will only display and group a folder of emails by the name, not the address, an email was sent to – and this doesn’t help for emails with multiple recipients. So I created an Applescript to trawl through my entire mailbox and find all of the email addresses for me.

This Applescript will save all of the recipient email addresses, ending in mydomain.com, to a text file. Click on your inbox (or any other folder) in Entourage’s Folders list, and then do “Select all” for the contents of that folder. (This is much quicker than selecting the emails manually.)

Here’s the script. You’ll need to change the value of thePath to be a valid path to a new text file to be created on your Mac.


tell application "Microsoft Entourage"
set selectedMessages to the current messages
if selectedMessages is {} then
return
end if
set thePath to "Macintosh HD:UsedEmailAddresses.txt"
set theFile to (open for access thePath with write permission)
try
set eof theFile to 0
repeat with theMessage in selectedMessages
set theRecipients to every recipient of theMessage
repeat with thisRecipient in theRecipients
set thisAddress to address of address of thisRecipient
if thisAddress ends with "mydomain.com" then
write (thisAddress & (ASCII character 13)) to theFile
end if
end repeat
end repeat
close access theFile
on error
close access theFile
end try
end tell

This will end up with many duplicate email addresses in the text file.

To pull out an unique list of the emails, I did the following:

  • Copied the text file’s contents into Excel 2004
  • Sorted the column of emails alphabetically using the “Data > Sort…” menu item in Excel
  • Added a new row at the top to act as a “header” for the column
  • Filtered the column using “Data > Filter > Advanced Filter…” to “Filter the list, in-place” with the “Unique records only” option checked.

This gave me my unique list of emails, which I then added to a whitelist on my email server. Anything sent to an email not on this list is instantly rejected. Hey presto, an instant reduction in spam.

I haven’t been able to test this in Entourage 2008, but the same script should work.