Setup Gmail IMAP, Mail.app, and the iPhone with Archive
Monday, November 17th, 2008If you’re happy using gmail.com as your user interface, then there’s no need to read further into this blog post. The Gmail web interface is the best I’ve ever used, but it’s not as good as a native, desktop app. If you wail on your applications like I do, then you need fast and local. Apple’s Mail.app isn’t prefect (no email client is), but it’s good enough. The interface is snappy, the search is fast and reliable, and I can drag and drop attachment files onto it.
I’m going to explain how to setup Gmail to work the right way with Mail.app and the iPhone. “Right”, of course, means “the way I want it to work.” In particular, this means that I want “deleting” something in my client to make it “archived” in Gmail for eternity. I almost never want an email to be deleted forever. I also want Sent Messages and Drafts to work in a reasonable way, but delete -> archive is the big one for me.
NB: This guide is a work in progress, a living document. Check back for updates. Feedback is actively solicited.
Setting up Mail.app
Follow the directions at the Gmail Help Center. Make sure you enable IMAP in the Gmail web interface before you play with Mail.app. Also, when Mail.app notices you’re setting up a Gmail account and offers to do it for you automatically, don’t fall for it. It will setup and POP3 account, which is worthless, and you’ll have to start over. Finally, note that every time you’re ever asked for a “User Name:” you’re going to have to enter your whole Gmail address
username@gmail.com.As always, the devil is in the details. In this case, the details are the “Mailbox Behaviors” panel for the account in Mail.app preferences. Google has Recommended IMAP client settings, an obscure Gmail Help Center page, and the Mail.app settings are unlinkably hidden by Javascript. I believe Google’s recommended settings are wrong. Well, I think their “Sent” recommendation is wrong, I agree with the other ones. Here is my recommendation.

If you match these settings then your Drafts, Sent, and Junk folders will automatically sync with the corresponding Gmail folders. Your local Trash folder will remain empty, and your deleted messages will get archived in Gmail’s “All Mail” folder.
If “Sent” is not checked, then Gmail store a copy in its Sent Mail folder, and Mail.app will store a copy in its Sent folder. These two copies will be different, and deleting one will not delete the other. The ”Delete sent messages when:” setting will only apply to the Mail.app copy. If that’s what you’re into, then go for it.
A few caveats
After following the above directions, Mail.app may or may not have figured out how to merge the the Gmail tags with the corresponding Mail.app folder. For example, the Gmail “Spam” tag and the Mail.app “Trash” folder. In the worst case, if Mail.app figures out nothing, then your Gmail setup in Mail.app’s main window will look like the image to the left. If this is the case, then click on a folder that Mail.app did not figure out for you. Let’s start with “Drafts”. Click on the “Mailbox” menu, pull down to “Use This Mailbox For”, and select “Drafts”. Repeat as necessary for “Sent Mail”, and “Spam”. Do not do this for “Trash”. If you do, then Mail.app will silently turn on “Move deleted messages to the Trash mailbox”__ and your archived mail will start getting silently deleted. Oofta. Also, for some reason my copy of Mail usually won’t merge “Spam” with it’s “Junk” folder. I’ve given up on trying to make it work, because it doesn’t really matter anyway.
You may have noticed that with the above settings you lose the Trash folder in Mail.app. That is, you will have no record of your recently deleted messages except for Gmail’s “All Mail” folder (Mail.app’s Trash will be forever empty). I’m fine with that, since deleted messages are the overwhelming majority of my “All Mail” folder. Another option is to make a costume tag in Gmail like “[Deleted Messages]”, and then set this to be the Trash folder in Mail.app. Then, when you delete the message Mail.app will remove all existing tags, and add a [Deleted Messages] tag. If you delete it againOther option. Make a [Deleted Messages] folder yourself.
Setting the “IMAP Prefix”
In the Mail.app sidebar your Gmail tags will appear as folders. The built in Gmail tags (All Mail, Spam, Starred, Trash) appear nested in a folder named [Gmail]. The rest of your tags appear as folders at the same level as the [Gmail] folder. Some people recommend that you set your IMAP path prefix (in Preferences -> Accounts -> Advanced -> IMAP path prefix:) to Gmail. I don not recommend this. It will make your built in Gmail tags appear at the top level, but it will make your personal tags disappear entirely. Also, if you fiddle with it Mail.app will re-download your entire mailbox. Fair warning.
Setting up iPhone Mail
First, and important FYI: there are two different and barely distinguishable types of Gmail accounts on the iPhone. If you go to “Mail, Contacts, Calendar” settings, then “Add account…”, and select “Gmail” then you will get an account type that I will call “POP3”, even though I’n not certain it is POP3. This potentially out of date video calls it POP3, though the obscure page calls it IMAP. Regardless of what it is, you can’t make it work the way we want.
Amazingly, if you let iTunes sync your Mail.app Gmail IMAP account to the iPhone, the iPhone will turn it into a maybe-POP3 account. Furthermore, if you have a mail account on the iPhone that looks like an account in Mail.app, but isn’t quite the same, then iTunes will be unhappy about syncing. So the first order of business is to stop iTunes from trying to sync your Mail.app Gmail account to your iPhone. Plug in your iPhone, find it in iTunes, click on the Info tab, scroll down to Mail Accounts, and uncheck your gmail account. Then, since you can’t plug in an iPod without it syncing, go delete the account from your iPhone.
The only way to get a Gmail IMAP account on the iPhone is to instead select “Other” at the account creation screen, and fill out the settings by hand. Instructions are on this page, or you can follow the above previously linked video. Note once again that you need to type out the full username@gmail.com each time.
After you’ve added your account information, quit the Settings app, then fire up Mail and let it download at least your folder structure. If you don’t do this, then the iPhone will let you fiddle with settings using only a fake INBOX folder that it assumes your account will have. Then, after it downloads your real folders, it will throw out the settings you put on the fake INBOX folder. I kid you not.
Once again, Google recommends that you keep a separate copy your sent messages on the iPhone on its Recommended IMAP client settings page, and I disagree. My settings are in the image below.
Other notes
This is a lightly tested theory: if you enter username instead of username@gmail.com then you will download new messages ok, but your deletes and folder moves will not propagate to the server correctly. Oofta.
Heinous iPhone Bug: If you make a new account with the same settings as an existing account, then the client will merge the two accounts with the new one’s blank settings, and erase the data in the old one. If you delete one, then it will delete both.
Advanced IMAP Settings
Google Labs recently released Advanced IMAP Controls for Gmail. If you activate advanced IMAP controls then you can choose to make some folders “invisible” to the IMAP interface. Some people are really excited about this, because they don’t want to download their huge “All Mail” folder. Mail.app will only download it once anyway (and then you have an offline copy), and the iPhone only downloadss 50 at a time, so I don’t see what the big deal is.
The advanced controls also expose a completely incomprehensible feature to let you “turn off auto-expunge”. When this came out about 100 people reblogged it, and it’s pretty clear that no one knows what it does or how it’s supposed to work. According to the official Google post:
The IMAP protocol allows messages to be marked for deletion, a sort of limbo state where a message is still present in the folder but slated to be deleted the next time the folder is expunged. In our standard IMAP implementation, when you mark a message as deleted, Gmail doesn’t let it linger in that state — it deletes (or auto-expunges) it from the folder right away. If you want the two-stage delete process, after you’ve enabled this Lab, just select ‘Do not automatically expunge messages’ under the ‘Forwarding and POP/IMAP’ tab in Settings.
Um, ok. First off, what is “expunge” supposed to mean, and what does it mean to Google? Second, if deleted messages aren’t immediately expunged, then when are they expunged? Once a month? When you click the “expunge” button? (There is no expunge button). Third, that option to “Do not automatically expunge messages” followed by “When a message is expunged from the last visible IMAP folder: Archive the message” is pretty tempting. It’s a trap! As far as I can tell, with every other permutation of settings this will make deleted messages go to the Trash and then disappear forever.
I held out a long time before making the move to Gmail. I got an invite soon enough to get my second favorite username, but then I sat on it for years without using it. At first it was because I was too hardcore to let the gRobot read my email. Later, when I’d become less of an idealist and more of a pragmatist, it was because Gmail didn’t support IMAP. When IMAP support was added last year, I didn’t move because I was to lazy to migrate. Eventually my other mail server went down at an inopportune time, and I decided to make the leap.
Pages with potentially useful information
Apple iPhone and Gmail deleted messages











