iPhone’s mail.app and IMAP folders
I use IMAP folders combined with fairly extensive Sieve rules to sort my incoming mail into the appropriate folders. This works great for me, because my Facebook notifications go to “Websites”, bill notifications go into “Bills”. It makes it easy for me to find what I’m looking for without having to sort through a single, massive Inbox.
When I got my iPhone, I was very disappointed in its handling of folders. It simply lacks the ability to check all folders for new mail. I didn’t want to have to click through each folder to see if I got new mail in one of them. I wanted to be able to see my little mail badge with the number of new messages I had, when I got a new message, no matter what folder it happened to reside in. I thought of various solutions, such as writing a server-side IMAP proxy that would do the checking for me and present a virtual mailbox of only new messages. While it may have been a fun project, a friend pointed out a more obvious solution: setup a second mailbox and have all of my mail forwarded to it. Here are the details of what I setup.
I created an account named “iphone” on my mail server. While I could have just added a .forward in my primary user’s home directory and called it done, I didn’t want to get everything sent to the mailbox on my iPhone. For example, SpamAssassin is setup to flag spam, and while I keep a “Spam” folder to periodically go back through for false-positives, I am not interested in seeing those on my iPhone. Also, I have several servers sending me logs occasionally. Again, I don’t care to see those on my iPhone. To keep the unnecessary stuff from being forwarded on, I implemented some rules to exclude the unwanted messages and forward the rest.
Here is an example for handling the messages marked as spam from my ~/.dovecot.sieve file:
require ["imapflags","fileinto","copy"];
if header :is "X-Spam-Flag" "YES" {
addflag "\\Seen";
fileinto "Spam";
stop;
} else {
redirect :copy "iphone@rugmonster.org";
}
I put all of the rules for things I don’t want to be sent to the iPhone’s mailbox before the “redirect” statement.
I happen to like Sieve for setting up mail delivery rules, but this could also be accomplished with procmail if your local delivery agent doesn’t support Sieve. You could also do this with any mail client that supports mail processing rules.
To prevent mail from being sent directly to the iPhone’s mailbox, I added a recipient check in my Postfix config. First, I created a file I called “denied_recipients” with the following:
iphone@ REJECT This address doesn't accept mail directly
I updated my smtpd_recipient_restrictions so Postfix would check that file. It looks like this now:
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
check_recipient_access hash:/etc/postfix/denied_recipients,
reject_unauth_pipelining,
reject_non_fqdn_recipient,
reject_unknown_recipient_domain,
reject_unauth_destination,
reject_rbl_client bl.spamcop.net,
reject_rbl_client zen.spamhaus.org,
permit
Since mail is being forwarded locally, everything gets through perfectly fine. And because I’m not accepting mail for that address, I can comfortably put iphone@rugmonster.org up on here and not worry about spammers sending me a bunch of crap.
While that all works great, I did one more thing to make things a bit easier for myself. I don’t always delete messages out of the mailbox on the iPhone on a regular basis. In fact, I would commonly delete the messages straight out of the maildir from the shell when the mailbox got too big. To keep the mailbox fairly tidy, I’ve setup a cronjob to clean things up for me.
0 0 * * * find /home/iphone/Maildir/ ! -type d -type f
-path "*/cur/*" -daystart -ctime +1 -exec rm -f {} \; 2> /dev/null/
Note that the newline is only for formatting on this page. cron does not permit commands to be split across multiple lines.
This just goes through the iphone mailbox’s Maildir and removes and messages that are older than 1 day. It gets not only the messages in the inbox, but also anything that I may have manually deleted. This isn’t necessary by any means, but I’m lazy. What sys admin isn’t, though.
