Mail Relay: IMAP to Gmail
Gmail is retiring POP fetching ("Get mail from other accounts"), so the old setup of pulling an external inbox into Gmail is going away. imap2gmail replaces it with a small long-running container that relays mail over IMAP instead.
Flow
Source IMAP -> read raw message -> append to Gmail -> delete from source -> optional ntfy
- The container selects the source inbox and keeps a state file with
uidValidityand the last processed UID. - New messages are fetched as raw RFC822 data and appended to the matching Gmail folder.
- Only after a successful append is the source message marked deleted and expunged.
- IDLE provides push delivery, with a fallback poll as a safety net.
Routing rules
- Remove list, matching messages are moved to the source trash and never delivered.
- Archive rules, matching messages go straight to Gmail All Mail without a notification.
- Spam, messages with a spam header can be routed to the Gmail spam folder.
- Other domains, mail that is not addressed to one of my domains can be filed into a separate folder.
Rules are evaluated in a fixed order: removal first, then archive, then spam, then the domain check.
Failure handling
- If an append fails, the message is counted as failed and the UID advances, so one bad message cannot block the queue forever.
- If the delete fails after a successful append, the container rolls back the Gmail copy by Message-ID.
- A crash between append and delete is recovered on startup through a pending UID in the state file.
- Optional ntfy notifications report successful deliveries, repeated failures and recovery.
Not the outbound relay
This container handles inbound mail only. Outbound mail from other containers, such as Watchtower notifications, still goes through the internal Postfix relay described on the Docker container strategy page.
Deployment
It runs as a Docker container with a persistent volume for the state file. See the imap2gmail repository for folder names, credentials and the compose example.
Mail addresses, credentials and folder names are not published here.