#+HUGO_SECTION: posts #+HUGO_BASE_DIR: ../ #+TITLE: Multiple Gmail accounts & labels with Emacs #+DATE: 2021-02-27 #+HUGO_DRAFT: false #+HUGO_TAGS: emacs #+HUGO_TAGS: mail #+PROPERTY: header-args :exports both * Intro For quite some time, e-mail seemed like an anomaly in my workflow. I am a long time Gmail user, and my decade-old account has a somewhat formidable quantity of labels and filters. My messages are often assigned multiple labels, and I also like to keep only a bunch of messages in the inbox. Although, in my opinion, Gmail web UI was and still is leagues ahead of many of its competitors and even allows keyboard-centric workflow, it's awkward to use with a keyboard-driven browser, and for no money on Earth I would enable browser notifications. Any classical IMAP/SMTP client is hard to use in my case, because a message with multiple labels is copied to IMAP folders for each of the label plus the inbox folder, and the copies look like different messages from the client-side. For example, a message can be read in one label and unread in another. For a few years, my solution was [[https://getmailspring.com/][Mailspring]], which provides first-class support for labels. However, it has a feature to deploy [[https://www.bbc.com/news/technology-56071437][spy pixels]] on emails (and offers no protection from them, obviously), the client is Electron-based with a mouse-driven interface, and the sync engine was closed-source at the time. So, I found an alternative in Emacs+notmuch+lieer and ditched one more proprietary app (the last big one I can't let go of is DataGrip). [[file:static/images/gmail/main.png]] [[file:static/images/gmail/mail.png]] Notmuch's tags are just as advanced as Gmail's labels, so I have basically the same mail structure accessible from Emacs, Gmail Android client and even the web UI when I don't have access to the first two. Also, I think the setup I describe here is pretty straightforward and less complex than many I encountered, but my impression is not the most reliable source of such knowledge. In any case, what follows is a description of my current workflow with instructions of varying levels of precision of how to get there. * Setting up ** Gmail Before we start, some setup is required for the Gmail account. First, as there is no way to enable SMTP without IMAP on Gmail, you have to set "Enable IMAP" in the "Forwarding and POP/IMAP" tab in the settings. If you use two-factor auth, generate an [[https://support.google.com/accounts/answer/185833?hl=en][app password]]. Also, make sure your labels do not contain whitespaces because if they do, you will have to type them in quotes all the time. ** lieer [[https://github.com/gauteh/lieer][lieer]] (formerly gmailieer) is a program that uses Gmail API to download email and synchronize Gmail labels with notmuch tags. Because of its usage of Gmail API instead of IMAP, there are no problems with duplicating emails in different labels, etc. As I need to use multiple versions of Python & Node.js for other reasons, I manage my installations of them with [[https://anaconda.org][Anaconda]] (Miniconda, to be precise). You may instead use [[https://docs.python.org/3/library/venv.html][venv]] or even the system-wide installation of Python and omit the =conda= clauses, but in my experience Anaconda makes life easier in that regard. #+begin_src bash :eval no # Create an environment with the name "mail" conda create --name mail # Activate the environment conda activate mail # Install Python conda install python # Download and install lieer git clone https://github.com/gauteh/lieer.git cd lieer pip install . #+end_src After which we may check if the =gmi= executable is available: #+begin_src bash which gmi #+end_src #+RESULTS: : /home/pavel/Programs/miniconda3/envs/mail/bin/gmi ** Notmuch [[https://notmuchmail.org/][Notmuch]] is present in most of the package repositories, so you can install it with your package manager, which is =pacman= in my case. #+begin_src bash :eval no sudo pacman -S notmuch #+end_src After the installation, run =notmuch setup=. That will inquire the parameters and create the =.notmuch-config= file with the answers. #+begin_src bash :eval no Your full name [Pavel]: Pavel Korytov Your primary email address [pavel@pdsk.(none)]: thexcloud@gmail.com Additional email address [Press 'Enter' if none]: Top-level directory of your email archive [/home/pavel/mail]: /home/pavel/Mail Tags to apply to all new messages (separated by spaces) [unread inbox]: new Tags to exclude when searching messages (separated by spaces) [deleted spam]: #+end_src It is important to set the =new= tag for the new messages instead of the default =unread= and =inbox=. Next, add the rule to ignore JSON files to the =[new]= section of the =.notmuch-config= file, so it would look like this: #+begin_src bash :eval no [new] tags=new ignore=/.*[.](json|lock|bak)$/ #+end_src That is needed to ignore the lieer config files. Although, as I have noticed, notmuch is generally pretty good at detecting wrong files in its directories, an explicit ignore rule won't hurt. Now, create the mail directory and run the [[https://notmuchmail.org/manpages/notmuch-new-1/][notmuch new]] command. As notmuch has probably already noticed you, it uses the [[https://en.wikipedia.org/wiki/Maildir][maildir]] format, which basically means that one message is stored in one file. #+begin_src bash :eval no # The same directory mentioned in the 4th question mkdir ~/Mail # Initialize notmuch notmuch new #+end_src ** Add an account After that, we can create a directory for a mail account and initialize lieer. #+begin_src bash :eval no cd ~/Mail # Use whatever name you want mkdir thexcloud cd thexcloud # Intialize lieer gmi init thexcloud@gmail.com #+end_src Running =gmi init= will run an OAuth authentication to your Gmail account. The credentials will be stored in =.credentials.gmailieer.json= file, so make sure not to expose it somewhere. We also can add a few settings for lieer, which will make life easier. First, dots seem to be less awkward to type than slashes for the nested tags: #+begin_src bash :eval no gmi set --replace-slash-with-dot #+end_src Then, we don't want the =new= tag to be pushed back to Gmail #+begin_src bash :eval no gmi set --ignore-tags-local new #+end_src Now we can finally download the mail directory. To initiate the download, run #+begin_src bash :eval no gmi sync #+end_src The first download can easily take several hours, depending on the size of your email and the speed of your internet connection, but subsequent runs will be much faster. The last thing to do here is to add the =gmi sync= command to notmuch's [[https://notmuchmail.org/manpages/notmuch-hooks-5/][pre-new hook]], so that the email will be synchronized on the =notmuch new= command. #+begin_src bash :eval no # Create the hooks folder mkdir -p ~/Mail/.notmuch/hooks # Create the file cd ~/Mail/.notmuch/hooks cat > pre-new < post-new < $CHECK_FILE #+end_src The script is launched with cron every 5 minutes: #+begin_src bash :eval no */5 * * * * bash /home/pavel/bin/scripts/check-email #+end_src Here's how the notification looks like: [[file:static/images/gmail/notification.png]] * Caveats - [[https://github.com/gauteh/lieer#caveats][lieer]] has an extensive list of caveats concerning Gmail API - Make sure that you understand the [[https://github.com/gauteh/lieer#changing-ignored-tags-and-translation-after-initial-sync][implications]] of lieer's =--ignore-tags-locally= and =--ignore-tags-remote= - If two of your accounts receive the same email, it will be stored as one email in notmuch, so tags from these accounts will be merged and pushed back on the next sync. To solve that, you can set tags from one account to be ignored on the rest of the accounts - A sent email is being downloaded again on the next sync. Not a great deal, but it is somewhat annoying to download recently sent attachments.