+++ title = "Email IMAP Setup with isync" author = ["Elia el Lazkani"] date = 2020-12-03 lastmod = 2020-12-03 tags = ["email", "isync", "imap"] categories = ["misc"] draft = false +++ The blog post "[Email Setup with isync, notmuch, afew, msmtp and Emacs]({{< relref "email-setup-with-isync-notmuch-afew-msmtp-and-emacs" >}})" prompted a few questions. The questions were around synchronizing email in general. I did promise to write up more blog posts to explain the pieces I brushed over quickly for brevity and ease of understanding. Or so I thought ! ## Maildir {#maildir} Let's talk **Maildir**. [Wikipedia](https://en.wikipedia.org/wiki/Maildir) defines it as the following. > The Maildir e-mail format is a common way of storing email messages in which each message is stored in a separate file with a unique name, and each mail folder is a file system directory. The local file system handles file locking as messages are added, moved and deleted. A major design goal of Maildir is to eliminate the need for program code to handle file locking and unlocking. It is basically what I mentioned before. Think of your emails as folders and files. The image will get clearer, so let's dig even deeper. If you go into a **Maildir** directory, let's say **Inbox** and list all the directories in there, you'll find tree of them. ```bash $ ls cur/ new/ tmp/ ``` These directories have a purpose. - `tmp/`: This directory stores all temporary files and files in the process of being delivered. - `new/`: This directory stores all new files that have not yet been _seen_ by any email client. - `cur/`: This directory stores all the files that have been previously seen. This is basically how emails are going to be represented on your disk. You will need to find an _email client_ which can parse these files and work with them. ## IMAP {#imap} The **Internet Mail Access Protocol**, shortened to **[IMAP](https://en.wikipedia.org/wiki/Internet%5FMessage%5FAccess%5FProtocol)**, is an > Internet standard protocol used by email clients to retrieve email messages from a mail server over a TCP/IP connection. In simple terms, it is a way of communication that allows synchronization between a _client_ and an _email server_. ## What can you do with that information ? {#what-can-you-do-with-that-information} Now, you have all the pieces of the puzzle to figure out how to think about your email on disk and how to synchronize it. It might be a good idea to dive a little bit into my configuration and why I chose these settings to begin with. Shall we ? ## isync {#isync} Most _email servers_ nowadays offer you an **IMAP** (**POP3** was another protocol used widely back in the day) endpoint to connect to. You might be using _Outlook_ or _Thunderbird_ or maybe even _Claws-mail_ as an _email client_. They usually show you the emails in a neat **GUI** (Graphical User Interface) with all the _read_ and _unread_ mail and the _folders_. If you've had the chance to configure one of these clients a few years ago, you would've needed to find the **IMAP** _host_ and _port_ of the server. These clients _talk_ **IMAP** too. [isync](https://isync.sourceforge.io/) is an application to synchronize mailboxes. I use it to connect to my _email server_ using **IMAP** and synchronize my emails to my hard drive as a **Maildir**. ### IMAP {#imap} The very first section of the configuration is the **IMAP** section. ```conf IMAPAccount Personal Host email.hostname.com User personal@email.hostname.com Pass "yourPassword" # One can use a command which returns the password # Such as a password manager or a bash script #PassCmd sh script/path SSLType IMAPS CertificateFile /etc/ssl/certs/ca-certificates.crt IMAPStore personal-remote Account Personal ``` In here, we configure the **IMAP** settings. Most notably here is of course `Host`, `User` and `Pass/PassCmd`. These settings refer to your server and you should populate them with that information. The `IMAPStore` is used further in the configuration, this gives a name for the **IMAP** _Store_. In simple terms, if you want to refer to your _server_ you use `personal-remote`. ### Maildir {#maildir} The next section of the configuration is the **Maildir** part. You can think of this as where do you want _your emails_ to be saved _on disk_. ```conf MaildirStore personal-local Subfolders Verbatim Path ~/.mail/ Inbox ~/.mail/Inbox ``` This should be self explanatory but I'd like to point out the `MaildirStore` key. This refers to _email_ on _disk_. So, if you want to refer to your _emails on disk_ you use `personal-local`. At this point, you are thinking to yourself what the hell does that mean ? What is this dude talking about ! Don't worry, I got you. ### Synchronize to your taste {#synchronize-to-your-taste} This is where all what you've learned comes together. The fun part ! The part where you get to choose how you want to do things. Here's what I want. I want to _synchronize_ my _server_ **Inbox** with my _on disk_ **Inbox** both ways. If the **Inbox** folder does not exist _on disk_, create it. The name of the **Inbox** on the server is `Inbox`. This can be translated to the following. ```conf Channel sync-personal-inbox Master :personal-remote:"Inbox" Slave :personal-local:Inbox Create Slave SyncState * CopyArrivalDate yes ``` I want to do the same with `Archive` and `Sent`. ```conf Channel sync-personal-archive Master :personal-remote:"Archive" Slave :personal-local:Archive Create Slave SyncState * CopyArrivalDate yes Channel sync-personal-sent Master :personal-remote:"Sent" Slave :personal-local:Sent Create Slave SyncState * CopyArrivalDate yes ``` At this point, I still have my _trash_. The _trash_ on the server is called `Junk` but I want it to be `Trash` on disk. I can do that easily as follows. ```conf Channel sync-personal-trash Master :personal-remote:"Junk" Slave :personal-local:Trash Create Slave SyncState * CopyArrivalDate yes ``` I choose to _synchronize_ my _emails_ both ways. If you prefer, for example, not to download the _sent_ emails and only _synchronize_ them up to the server, you can do that with `SyncState`. Check the `mbsync` manual pages. ### Tie the knot {#tie-the-knot} At the end, add all the channel names configured above under the save _Group_ with the same account name. ```conf Group Personal Channel sync-personal-inbox Channel sync-personal-archive Channel sync-personal-sent Channel sync-personal-trash ``` ## Conclusion {#conclusion} This is pretty much it. It is that simple. This is how I synchronize my email. How do you ?