Introduce class-based architecture with inheritance for IMAP clients: - ImapClient base class with private low-level methods - SourceImap and DestImap subclasses for specific operations - EmailForwarder orchestrator class for coordination - Global load_config and main functions Improvements: - Clear separation of concerns and encapsulation - Private methods for internal IMAP calls - Better error handling and logging - Maintains all original functionality |
||
|---|---|---|
| email_forwarder.service.d | ||
| .env.example | ||
| .gitignore | ||
| email_forwarder-fail-notify.service | ||
| email_forwarder.service | ||
| email_forwarder.timer | ||
| install.sh | ||
| main.py | ||
| pyproject.toml | ||
| README.md | ||
Email Forwarder
This script forwards new emails from a source IMAP account (e.g., GMX) to a destination IMAP account (e.g., MXRoute), avoiding duplicates by tracking Message-IDs and using time-based searches.
Setup
Automated Install (Recommended)
Run the install script: ./install.sh
This will set up the virtual environment, systemd services, and provide post-install instructions.
Manual Setup
-
Copy
.env.exampleto.envand fill in your IMAP credentials, Uptime URLs, and settings. -
Create virtual environment:
python -m venv venv -
Activate and install:
source venv/bin/activate && pip install -e . -
For user services (recommended):
- Copy services:
cp *.service *.timer ~/.config/systemd/user/ - Copy drop-in:
cp -r email_forwarder.service.d ~/.config/systemd/user/ - Copy
~/.config/systemd/user/email_forwarder.service.d/override.conf.sampletooverride.confand edit to set your PROJECT_DIR and ENV_FILE paths.
- Copy services:
-
Reload and enable:
systemctl --user daemon-reload && systemctl --user enable email_forwarder.timer && systemctl --user start email_forwarder.timer -
Check status:
systemctl --user status email_forwarder.timer
Configuration (.env)
SOURCE_HOST: Source IMAP server (e.g., imap.gmx.com)SOURCE_PORT: Port (default 993)SOURCE_USER: Source email/usernameSOURCE_PASS: Source passwordDEST_HOST: Destination IMAP server (e.g., yourserver.mxrouting.net)DEST_PORT: Port (default 993)DEST_USER: Destination email/usernameDEST_PASS: Destination passwordFOLDERS: Folders to sync (default INBOX; use "all" for all folders or comma-separated list like "INBOX,Sent")DRY_RUN: Set totruefor testing without forwarding (default false)UPTIME_SUCCESS_URL: URL for success pingUPTIME_FAIL_URL: URL for failure ping
How It Works
- Uses time-based search (
SINCE) withlast_run.txtto find emails since last run. - Extracts Message-ID, checks against local file and destination IMAP for duplicates.
- Forwards via IMAP APPEND; updates
last_run.txton success. - Logs activity; systemd handles Uptime Kuma pings.
Monitoring with Uptime Kuma
- Success: Pings
${UPTIME_SUCCESS_URL}after forwarding. - Failure:
OnFailurepings${UPTIME_FAIL_URL}.
Dependencies
- imaplib, email (built-in)
- python-dotenv, imap-tools
- systemd (for user services)
Troubleshooting
- Logs:
journalctl --user -u email_forwarder.service - Test:
source venv/bin/activate && python main.py - Timezone issues: Adjust UTC offset in script if needed.