Setting up isync (mbsync) on Linux

Brian Thompson
3 min readAug 5, 2021

--

Most of this information is taken directly from the isync documentation.

isync is a command-line application that synchronizes mailboxes; currently, MailDir and IMAP4 mailboxes are supported. New messages, message deletions, and flag changes can be propagated both ways. isync is suitable for use in IMAP-disconnected mode.

Synchronization is based on unique message identifiers (UIDs), so no identification can occur (as opposed to some other mail synchronizer). Synchronization state is kept in one local text file per mailbox pair; multiple replicas of a mailbox can be maintained.

Features

  • Fine-grained selection of synchronization operations to perform
  • Synchronizes single mailboxes or entire mailbox collections
  • Partial mirrors possible: keep only the latest messages locally
  • Trash functionality: backup messages before removing them

IMAP features:

  • TLS/SSL via IMAPS and STARTTLS for encryption
  • SASL for authentication
  • Pipelining for maximum speed

Compatibility

isync should work fairly well with any IMAP4 compliant server; particularly efficient with those that support UIDPLUS and LITERAL+ extensions. Some servers require workarounds.

Usage

While isync is the project name, mbsync is the current executable name. This change was necessary to enable a transition period after massive changes in the user interface.

Requirements

  • An email address on a host that supports IMAP (e.g., ProtonMail, Gmail)
  • The mbsync executable (“sudo apt install isync”)
  • A .mbsyncrc file in the user’s home directory

.mbsyncrc setup

I will be using ProtonMail for this example. ProtonMail provides a tunnel, so there is no need to enable SSL with mbsync.

Create Both
Expunge Both
SyncState *
IMAPAccount user
Host 127.0.0.1
Port 1143
User user.email@protonmail.com
Pass "<your IMAP password>"
SSLType None
IMAPStore user-remote
Account user
MaildirStore user-local
Subfolders Verbatim
Path ~/mail/user
Inbox ~/mail/user/inbox
Channel user-default
Master :user-remote:
Slave :user-local:
Patterns * ![Protonmail]*
Create Both
SyncState *
Sync All
Group user
Channel user-default

Run “mbsync -a” to synchronize all your mailboxes. You just need to create the directory defined by Path (in this case it is ~/mail/user) before running mbsync.

Breaking it down

  • “Create Both”: Automatically create missing mailboxes locally. Otherwise, print an error message and skip that mailbox pair if a mailbox and the remote do not exist.
  • “Expunge Both”: Permanently remove all messages locally and remotely marked for deletion.
  • “SyncState *”: Set the location of this Channel’s synchronization state files. “*” means that the state should be saved in a file named .mbsyncstate locally; this has the advantage that you do not need to handle the state file separately if you delete the mailbox, but it works only with Maildir mailboxes. Otherwise, this is interpreted as a string to prepend to the local mailbox name to make up a complete path. This option can be used outside any section for a global effect. In this case, the appended string is made up according to the pattern :remote-store:remote-box:local-store:local-box.
  • “IMAPAccount”: Define the IMAP4 Account name.
  • “Host-Pass”: Self-explanatory.
  • “SSLType None”: Select the connection security/encryption method. None is for no security. This is the default when Tunnel is set, as tunnels are usually secure. STARTTLS enables STARTTLS security after connecting the regular IMAP port 143. Most servers support this, so it is the default unless a tunnel is used. IMAPS establishes security by starting SSL/TLS negotiation right after connecting the secure IMAP port 993.
  • “IMAPStore”: Define the IMAP4 store name.
  • “Account”: Specify which IMAP4 Account to use. Instead of defining an Account and referencing it here, it is also possible to specify all the Account options directly in the Store’s section.
  • “MaildirStore”: Define the Maildir Store name.
  • “Subfolders Verbatim”: The on-disk folder naming style used for hierarchical mailboxes. This option has no effect when Flatten is used. Verbatim is the style you probably want to use. Other options include Maildir++ and Legacy.
  • “Channel”: Define the channel name.
  • “Patterns”: Instead of synchronizing only one mailbox pair, synchronize all mailboxes that match the patterns. The mailbox names are the same on the remote and local hosts. Patterns are IMAP4 patterns.

Summary

isync can be set up easily, and there are other articles that describe how to set up isync with other mail providers, such as Gmail. In the next article, I will describe how to set up mutt, and in a later article, I will describe how to combine isync with mutt so you can get the most out of your IMAP server.

--

--