Isync - Mailbox Syncing

Posted on Mar 30, 2023

Home Page: https://isync.sourceforge.io/

Isync is the name of the project for the tool mbsync which is used to sync your email mailboxes. Basically it ensures that your emails on your computer match what is on your email providers server. Note that this is a command line only tool.

Installation

Use your systems package manger. See below for examples:

## Arch
sudo pacman -S isync
## Void
sudo xbps-install -S isync
## Debian/Ubuntu
sudo apt install isync
## macOS
brew install isync

Configuration

Out of box isync/mbsync will not work and does require some configuration on the users part. Thankfully the configuration is really not that complicated. In fact you only need to create one configuration file. Read below for a walk through of how to configure isync/mbsync.

For our example we will be using the following info, be sure to use your own information when setting up your configuration:

Email: user@example.com

Password: password123

Email Host: mail.example.com

Local Computer Mail Folder (This can be anywhere, I use the following): /home/user/.mail/

Configuration - Create you local mail directory

This is the directory that you will be storing your emails locally. I like to create a parent directory called “.mail” which houses the directory to all my mail accounts. In our example we are only using one mail account but if you are planning on using multiple accounts then repeat the below command for each mail account adjusting as needed.

mkdir -p ~/.mail/user@example.com

Configuration - Create .mbsyncrc

Create .mbsyncrc using your preferred editor and then add the below configuration. I will be using Vim.

# Create and edit .mbsyncrc using Vim
vim ~/.mbsyncrc
# ~/.mbsncrc

IMAPAccount user #Name your account. Can be anything. 
Host mail.example.com #Enter your email providers IMAP host address. Typically this is something like imap.gmail.com or imap-mail.outlook.com.
Port 993 #This can either be 993 or 143 depending on your connection type.
User user@example.com #This is your actual email address.
Pass password123 #This is your email password.
AuthMechs LOGIN #This declares what type of authentication is used by the email server, in most cases you will use LOGIN.
SSLType IMAPS #Sets the connection type. In our case we are using IMAPS (IMAP over TLS) but in some cases can be STARTTLS.
CertificateFile /etc/ssl/certs/ca-certificates.crt #The location of your cert files.

IMAPStore user-remote #Name your IMAPStore. Can be anything really.
Account user #Set your account to use. This is in reference to the above settings.

MaildirStore user-local #Name your MaidldirStore. Can be anything really.
Subfolders Verbatim #Sets the naming style used for hierarchical mailboxes. Verbatim sets it to Path/top/sub/subsub and Inbox/sub/subsub.
Path /home/user/.mail/user@example.com/ #This is the path to the directory of your local mail.
Inbox /home/user/.mail/user@example.com/INBOX #This is the path to the directory of your local INBOX folder.

Channel user #Name the Channel. Can be anything really.
Expunge Both #This makes it so all files marked for deletion on both the mail server and the local machine are deleted.
Far :user-remote: #Set this to the name of your IMAPStore you set above.
Near :user-local: #Set this to the name of your MaildirStore you set above.
Patterns "INBOX" "*" #This sets what mailboxes to sync. The "*" syncs anything and all mailboxes on your server.
Create Both #This creates all missing mail boxes on both the local machine and the server.
Sync All #This executes all sync operations.
SyncState * #Sets the location of this channels sync state. Default is .mbsyncstate.
# End profile

Configuration - Secure Your Password (Optional)

The following steps are not necessary but strongly recommend. Isync/mbsync allows you to either place your password in the .mbsyncrc configuration file or call an external command to handle the password input. If you choose to simply place your password in the .mbsyncrc file know that your password will be stored in plaintext, meaning that if anyone gets their hands on your .mbsyncrc file then they will also have your password. The better option is to call an external command. I will show two methods for how to do this, see below:

Configuration - Secure Your Password (Optional) - Method 1

Method 1 - GPG

The first method is to use GPG to encrypt a file storing your password. Basically you will create a regular file and paste your email password into the file. You will then encrypt the file with a password so that only those who know the encryption password can access the email password.

read -s password
echo $password > ~/.isyncpass
gpg -c ~/.isyncpass
shred -u ~/.isyncpass
#Replace "Pass password123" in your ~/.mbsyncrc with the below.
PassCmd "gpg -q --for-your-eyes-only --no-tty -d ~/.mailpass.gpg"

Configuration - Secure Your Password (Optional) - Method 2

Method 2 - Pass

The second method is to use pass. Pass is a password manager for Unix systems and does require some initial setup to get it working, please lookup its manual for how to set it up. Basically pass works like any other password manager, you add your email password to it and secure it using a master password. To access the email password you need to know the master password; in reality there’s more to it but that’s the gist of it.

pass add user@example.com
#Replace "Pass password123" in your ~/.mbsyncrc with the below.
PassCmd "pass user@example.com"

Usage

Using isync/mbsync is really easy. You just need to run the below commands.

#Sync a specific channel.
mbsync -V user
#Sync all channels.
mbsync -a

End

That pretty much sums up isync/mbsync, it really is a powerful tool that you can be used for all sorts of applications.