Prerequisites
Install isync using your package manager, here we are installing the apt package manager, but use whatever package manager you are using:
sudo apt update && sudo apt install isyncWe will use isync program to download mails to local Maildir folder. We can also use a program called offlineimap which is a bit slower but it can work on Windows.
NOTE: isync is the name of the project, mbsync is the name of the executable.
Isync Configuration
Download mails from remote Maildir folder using .mbsyncrc configuration file with following settings.
Remote Maildir
Arbitrary name to remote mail server:
### This file is generated by Emacs's "org-bable-tangle" function
## Gmail - vithurshan@laposte.net
IMAPAccount laposteImap server to download mails from:
Host imap.laposte.netID or mail address to connect to IMAP server:
User vithurshan@laposte.netDo not use clear text password Pass yourPassword. To store the password in an encrypted file use PassCmd "gpg -q --for-your-eyes-only --no-tty -d ~/.mailpass.gpg". Or use password manager Pass using:
PassCmd "/usr/bin/pass mail/laposte | head -n 1"Use SSL with IMAP protocol:
SSLType IMAPSWe can also set SSLVersions to use and the value is depends on the output of:
openssl s_client -connect imap.gmail.com:993 -showcerts | grep ProtocolThe following line should work. If you get certificate errors, uncomment the two following lines and read the "Troubleshooting" section at https://wiki.archlinux.org/title/isync#Troubleshooting:
CertificateFile /etc/ssl/certs/ca-certificates.crt
#CertificateFile ~/.cert/imap.gmail.com.pem
#CertificateFile ~/.cert/Equifax_Secure_CA.pemRemote Maildir as IMAPStore
Here we assign the remote maildir as IMAPStore:
IMAPStore laposte-remote
Account laposteLocal Maildir
Let's now configure local maildir location for storing mails locally by setting a arbitrary name for the section:
MaildirStore laposte-localThis replicates the hierarchy of subfolders on remote server to local maildir if there are any under the inbox:
SubFolders VerbatimThe path to store downloaded mail. The trailing "/" is important:
Path ~/.local/share/mail/vithurshan@laposte.net/
Inbox ~/.local/share/mail/vithurshan@laposte.net/InboxMails Sync Config
Associate mailboxes that we created earlier, local and remote, namely laposte-remote and laposte-local:
Channel laposte
Far :laposte-remote:
Near :laposte-local:The following configuration tells what and how to sync the local maildir with remote one. We can exclude everything under [Gmail] folder, except the interesting folders:
Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail"Some older accounts name Bin as Trash, and if the names of the remote folders are not mentioned correctly in the configuration file, then it will causes an error. Or if your Gmail is set up with a different lanugage, then you need to translate the names of these folders. For Norwegian [Gmail]/Corbeille would be [Gmail]/Papirkurv. We can view the names of remote folders:
mbsync -c ~/.emacs.d/mu4e/.mbsyncrc -Dmn acc1-gmailOr include everything:
Patterns *Automatically create missing mailboxes, both locally and on the server. For ex: if we create a folder in the remote or local maildir, the next time mbsync run it won't create the folder:
Create BothSync the movement of messages between folders and deletions, add after making sure the sync works:
Expunge BothSave the synchronization state files in the relevant directory (in local mailbox folder). For example if we move all of downloaded mails another location on the disk and the next time we run mbsync, it will read default sync state file and assume that we delete all mails and make that change to remote repo.
SyncState *Additional Mail Account [Optional]
We can download emails from as many remote locations as we want, here is a full Isync config of another IMAP server to download mails from:
## Gmail - vithurshanselvarajah@gmail.com
IMAPAccount gmail
Host imap.gmail.com
User vithurshanselvarajah@gmail.com
PassCmd "cat ~/lab/emacs/mu4e/password"
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt
IMAPStore gmail-remote
Account gmail
MaildirStore gmail-local
Subfolders Verbatim
Path ~/.local/share/mail/vithurshanselvarajah@gmail.com/
Inbox ~/.local/share/mail/vithurshanselvarajah@gmail.com/Inbox
Channel gmail
Far :gmail-remote:
Near :gmail-local:
Patterns * ![Gmail]* "[Gmail]/Messages envoy&AOk-s" "[Gmail]/Suivis" "[Gmail]/Tous les messages" "[Gmail]/Corbeille" "[Gmail]/Brouillons"
Create Both
SyncState *NOTE: Be careful of how you manage whitespace between lines in this file, the spaces define groupings!
NOTE: mbsync won’t create the base maildir for you, you’ll have to create it: ~/.local/share/mail/vithurshanselvarajah@gmail.com/
Start synchronization
Once we have put all the configuration in mbsyncrc file and created the folder ~/.local/share/mail/vithurshan@laposte.net/ to store the remote maildir locally, we are ready to download:
mbsync -c ~/.dotfiles/.config/isync/mbsyncrc -aAutomatic Sync [Optional]
We can automate the synchronisation of mailboxes using Systemd. Let's first a create a Systemd service file ~/.config/systemd/user/mbsync.service that launches the sync:
[Unit]
Description=Mailbox synchronization service
[Service]
Type=oneshot
ExecStart=/usr/bin/mbsync -Va -c /path/to/mbsyncrc -a
Environment="DISPLAY=:0"
# Environment="XAUTHORITY=/path/to/.Xauthority"
[Install]
WantedBy=default.targetNOTE: Make sure to change the value /path/to/mbsyncrc to Isync's configuration file that we created above.
Then creating ~/.config/systemd/user/mbsync.timer file which configures mbsync to be started 2 minutes after boot, and then every 5 minutes:
[Unit]
Description=Mailbox synchronization timer
[Timer]
OnBootSec=2m
OnUnitActiveSec=5m
Unit=mbsync.service
[Install]
WantedBy=timers.targetOnce those two files are created, reload systemd, then enable and start mbsync.timer:
systemctl --user daemon-reload && systemctl --user enable --now mbsync.timer