How to Save Emails So You Keep Them for Good
On this page
Email stored in Gmail, Outlook, or another mail service remains tied to that account. Archiving a message inside the service usually just removes it from the inbox; it does not create a separate copy. To keep email outside your account, save messages as files, back them up in separate places, and check that you can open them later.
mailmd is a free, open-source tool you run in a terminal to copy email into Markdown files on your computer. It works with common IMAP mailboxes, Gmail, Outlook.com, and Microsoft 365. The steps below show how to create a local archive and protect it over time.
Why archiving and syncing are not backups #
In Gmail, archived messages remain in All Mail. That helps organize an inbox, but the messages still depend on the Gmail account. Likewise, an email app that uses IMAP, a method for syncing email with a server, can copy deletions between the two. So a message visible on your computer is not necessarily protected from deletion online. Gmail explains what archiving does, and Mozilla describes how IMAP synchronization handles deletions.
A separate export or local archive gives you files outside the mail service. A backup is another copy of those files, kept so you can recover them if the computer or archive is lost. For long-term access, preserve the message details and attachments as well as the visible text. Email preservation guidance recognizes email file formats such as EML and MBOX. Each format serves different needs, and no single format preserves every feature in every program. The Digital Preservation Coalition discusses email format trade-offs.
Save email with mailmd #
Install mailmd in a terminal on Linux or macOS, or in Git Bash on Windows:
curl -fsSL https://raw.githubusercontent.com/vincentsch/mailmd/main/install.sh | shConnect the mailbox using the command that matches it:
mailmd connect imap --email you@example.com
mailmd connect gmail --client-secret client_secret.json
mailmd connect outlook --client-id <id>Use the IMAP command for IMAP mailboxes, including Gmail with an app password, a password created for an email app. To connect through Gmail's API, a way for software to access Gmail, provide credentials for your own OAuth app from Google Cloud. Outlook.com and Microsoft 365 connect through Microsoft Graph, Microsoft's service for accessing account data. This connection requires an OAuth app from Microsoft Entra to authorize access. Your provider may require an app password for IMAP; mailmd indicates when one is needed.
Then copy the messages:
mailmd sync --keep-rawmailmd saves the archive under ~/mail/<account>. Each message becomes a readable text file in Markdown format. It includes message details, called headers, such as sender, recipient, date, and subject. HTML email is converted to readable Markdown, and attachments are saved alongside the message file. The --keep-raw option also saves the original EML file. For Gmail over IMAP, mailmd syncs All Mail so archived messages are included; spam and trash are left out unless requested.
The tool opens mailboxes in read-only mode, so it can read messages but cannot change them. It asks only for read access. It does not mark messages as read, move or delete them, or send email. It also never deletes files from the archive, even when a message is later deleted from the server. Sync again to add new messages and update each message's read or starred status. For ongoing copies, schedule mailmd sync --quiet with your computer’s scheduler.
Keep the archive recoverable #
Back up the archive in more than one place. The Canadian Centre for Cyber Security recommends the 3-2-1 approach: keep three copies on two types of storage, with one copy off-site. A practical setup keeps the archive on your computer and a copy on an external drive, which you disconnect between backups. Keep the third copy in a separate location. The Centre’s backup guidance also recommends testing recovery.
Open some archived messages and attachments, including older ones, to confirm they work. Repeat the check at least once a year, as the Library of Congress recommends for personal email archives. Keep in mind that mailmd does not include its own search engine: search the files with an editor or note app, or use tools such as rg or grep.
Protect the archive like other sensitive personal files. mailmd saves login information outside the archive; those credentials are readable only by your user but are not encrypted. Secure the computer and any backup locations accordingly. Do not delete the original messages until you have checked the archive, opened important attachments, and confirmed that you can restore a backup.
Free and open source
Try mailmd
mailmd is a free command-line tool that copies your email into Markdown files on your computer, one file per message. It works with any IMAP mailbox, and with Gmail and Outlook through their own APIs.
Install
$ curl -fsSL https://raw.githubusercontent.com/vincentsch/mailmd/main/install.sh | sh
Set it up with your coding agent
Set up mailmd so you can read my email. Use https://github.com/vincentsch/mailmd: read its AGENTS.md and follow the steps. Connect my mailbox and run the first sync. Then show me where the files are.