Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically file email in Mail based on sender's domain Apps
While I'm a huge fan of Smart Folders in Mail, I've possibly been abusing them and not filing my mail at all. Once I had around 10,000 messages in my inbox, I decided that it was time to do something about it. Ideally, I'd have multiple sub-folders, such as Clients and Vendors, and then in these folders, I'd have Client Name A, Client Name B... and Vendor Name A, Vendor Name B, etc.

Sorting through 10,000 emails and manually moving them wasn't my idea of fun -- even on a cold, rainy afternoon. Neither was creating a slew of individual rules for something like If from contains clientname.com then move message to mailbox Client Name. This seemed like a job for AppleScript! As my script-fu isn't as good as I would like it, I've resorted to having a single Sorted folder, and then within that are folders named with the relevant domain name -- eg clientname.com. Here's the script:
tell application "Mail"
  set theList to the selection as list
  repeat with theMessage in theList
    -- first we find the sender of the message
    set theSender to sender of theMessage as string
    -- now we strip off the sender's domain (everything after the "@")
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"@"}
    set theDomain to the second text item of theSender
    set AppleScript's text item delimiters to ""
    -- and strip the last character off the domain, as it will be a ">"
    set theDomain to get characters 1 through ((theDomain's length) - 1) of theDomain
    if theDomain = "" then set theDomain to "Undetermined"
    set targetMailbox to "Sorted/" & theDomain
    try
      move theMessage to mailbox targetMailbox of account "IMAP Account Name" of application "Mail"
    end try
  end repeat
end tell
I then have a single rule in Mail that applies this AppleScript to all incoming emails, and if I've created a folder with the relevant domain name, the emails will automatically end up in there. As it uses a try, if the folder doesn't exist, then the message isn't moved.

I would welcome additions to this script from the MacOSXHints community -- my thoughts to extend it would be to use AppleScript's ability to read plist files, and have a plist file containing clientname.com -> Client Name mappings, and also allow for it to be sorted with finer granularity -- eg, having Friends, Clients, Vendors folders. Such an interface could (relatively) easily be created in AppleScript Studio, and have Mail just call the script to do the heavy lifting.
    •    
  • Currently 2.00 / 5
  You rated: 1 / 5 (5 votes cast)
 
[8,518 views]  

Automatically file email in Mail based on sender's domain | 6 comments | Create New Account
Click here to return to the 'Automatically file email in Mail based on sender's domain' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically file email in Mail based on sender's domain
Authored by: Coumerelli on Jan 30, '08 07:50:47AM

meh, I've only got 9,366 in my inbox. I've got a good coupla weeks before I need this. ;)

Sounds like a good way to sort.

---
"The best way to accelerate a PC is 9.8 m/s2"



[ Reply to This | # ]
Automatically file email in Mail based on sender's domain
Authored by: Anonymous on Jan 30, '08 08:47:50AM

"my thoughts to extend it would be to use AppleScript's ability to read plist files"
Why not use the Address Book data?
Look up the email address, and grab the company name (if it exists) for that email?



[ Reply to This | # ]
why not use Mail Rules for this?
Authored by: alexmathew on Jan 30, '08 11:45:55AM

I dont get it. You can choose to have rules for different domains - yes if you have hundreds of domains, then this script might help - but most of us or me atleast - have about 10 or 15 domains - and adding a rule is "safer" - I dont muck around with Applescript which I never seem to have the time to learn!



[ Reply to This | # ]
I meant Mail Rules without the Script...
Authored by: alexmathew on Jan 30, '08 11:48:36AM

On second thoughts this script may be useful if one gets mail from senders which you dont want to deal with right away! Still mucking about with AS in mail is still scary to me :-)
AM



[ Reply to This | # ]
Automatically file email in Mail based on sender's domain
Authored by: kaih on Jan 30, '08 01:06:07PM
There was a problem with the script as originally posted - as it is in the hint, then it works when invoked from the Script Editor, where I was doing my testing, however I was a little too hasty in posting it.

Here's an amended version that works when it's invoked as a rule in Mail.

using terms from application "Mail"
	on perform mail action with messages newMessages
		tell application "Mail"
			repeat with theMessage in newMessages
				-- first we find the sender of the message
				set theSender to sender of theMessage as string
				-- now we strip off the sender's domain (everything after the "@")
				set oldDelimiters to AppleScript's text item delimiters
				set AppleScript's text item delimiters to {"@"}
				try
					set theDomain to the second text item of theSender
					-- always set the text item delimiters back as soon as possible!
					set AppleScript's text item delimiters to ""
					-- and strip the last character off the domain, as it will be a ">"
					set theDomain to get characters 1 through ((theDomain's length) - 1) of theDomain
				on error
					set theDomain to "Undetermined"
					set AppleScript's text item delimiters to ""
				end try
				set targetMailbox to "Sorted/" & theDomain
				try
					move theMessage to mailbox targetMailbox of account "Emperor's Mind" of application "Mail"
				on error
					-- do nothing
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

This works well for me, as I also have one Smart Mailbox called Action Items that gathers all unread and all flagged emails - then all I need to do is monitor this one Smart Mailbox to see what emails need to have something done about them. Once I've read an email, if it needs a follow-up, I flag it. If not, it turns unread, and is automatically filed in the appropriate mailbox for future reference.

---
k:.

[ Reply to This | # ]

Automatically file email in Mail based on sender's domain
Authored by: TicToc on Jan 30, '08 02:44:46PM
I use POPFile http://popfile.sourceforge.net/ to do this sorting - set up POPFile buckets for each folder you want messages to end up in, and then train POPFile to do the sorting. The nice thing about this is that it will learn over time that Joe@client1.com also sometimes sends you email from his gmail account joe@gmail.com, and that this should be filed in the client1 folder. Plus POPFile is a pretty decent spam filter as well :)

Although I use the mac.com IMAP server and have POPFile watch & sort there, it works great when asked to sort incoming mail from a POP server. The particularly smart feature when using IMAP is that simply moving mail from one folder to another actually trains POPFile.

[ Reply to This | # ]