I'm running 10.6.8 and I set up a smart mailbox with two conditions: one, to look in a specific folder, and two, to match any recipient = recipient2@foo.com.

Problem:

Mail.app doesn't seem to search the entire actual email message. Neither does Spotlight, which might be the crux of the matter. And so I can't search for the envelope-recipient address in some messages, and that routing address does sometimes differ from the header-recipient (the one in the To: field).

Background:

It may help, in this case, to consider an email as being similar to a physical envelope. The front of the envelope contains routing information, handled by the mail-transfer agent (e.g. sendmail, postfix, qmail, Exchange). The inside letter contains formal addresses handled by the reader, which in this case is the mail client.

So, Mail.app doesn't necessarily have to care about the routing info -- the envelope-from address, which is Return-Path:, and the envelope-recipient address, which is shown in the Received: header -- it just cares about the To:.

I'm not sure what you're trying to do is possible through the smart mailboxes feature, but if you set up a Mail rule, you can filter based on "To". Additionally, you can access the headers of a Mail message in Applescript, so writing a script that searches the headers of all messages might get you what you need.
–
VickashNov 24 '11 at 15:42

Was just re-reading your question and realized you DON'T want to search by the "To" header, you want to search the "Received" header. Still possible in Applescript, but also using a Mail rule. Add a rule and scroll down to the end of the list when setting up a condition. Select "Edit Header List". You'll be able to add the "Received" header in there and define your condition with it. It still won't show up in the conditions for Smart Mailboxes though. Funny that these 2 should be different.
–
VickashNov 24 '11 at 15:49

Thanks, Vickash. I now see where 'Received' can be added to a rule. Unfortunately my created rule can't be applied to a non-Inbox IMAP folder (which was my oiginal intent); when I select a folder (in this case, 'Spam') and I go to the Message menu in Mail.app, the 'Apply Rules' selection is greyed out. Almost there, but not quite.
–
JubalNov 25 '11 at 16:52

I'll keep poking and probably file a request for enhancement with Apple's bug reporter. I see no reason why we can't customize header fields in smart mailboxes. I also wonder if it may be possible to modify the underlying Spotlight extension for mail messages. I've been reading macdevcenter.com/lpt/a/6403 and it appears kMDItemRecipientEmailAddresses (output of mdls <path-to-file.emlx>) contains the values I'd expect the envelope-recipient to appear in, but only contains the header-recipients.
–
JubalNov 25 '11 at 16:52