title: MBSync Example Config 1 tags: mail sync mbsync config The aim here is to download all messages from a given IMAP mailbox. To then delete from the server, move the messages from `cur`, `new`, to something like a directory called `arc`. Then `mbsync` will notice that the messages have disappeared from `new` and `cur`, and expunge them from the server. I generally have email accounts on my webhost to redirect things to, along with forwarding to my main email account. Periodically I want to clear everything out, download it, and stick it on a backup drive where, most likely, it will never be touched again. But if necessary I can search and index locally as I see fit. # Template ``` IMAPAccount EMAILM # Address to connect to Host IMAPHOST User EMAILA Pass PASSWORD # Use TLS SSLType IMAPS # The following line should work. If you get certificate errors, uncomment the two following lines and read the "Troubleshooting" section. CertificateFile /etc/ssl/certs/ca-certificates.crt IMAPStore EMAILM_remote Account EMAILM MaildirStore EMAILM_local SubFolders Verbatim Path ~/Mail/EMAILA Inbox ~/Mail/EMAILA/Inbox Channel EMAILM_ch Far :EMAILM_remote: Near :EMAILM_local: Patterns * Expunge Far ``` # Python Helper ```py #!/usr/bin/env python template = open("template.mbsyncrc").read() import sys import os imaphost = os.getenv("IMAPHOST","my.domain.com") args = sys.argv[1:] import re for arg in args: email,password = arg.split(":") emailm = re.sub(r"[^A-Za-z0-9]","_",email) output = template.replace("EMAILA",email).\ replace("EMAILM",emailm).replace("PASSWORD",password).\ replace("IMAPHOST",imaphost) with open(f"{email}.mbsyncrc","wt") as f: print(output,file=f) os.makedirs(email,exist_ok=True) print(f"Made {email}") ```