| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Self-hosted email fetcher: IMAP (ISP mailbox) → Gmail API import. Replaces Gmail’s deprecated POP3 fetch.
Requirements: Python 3.11+, IMAP credentials, Google Cloud project with Gmail API and OAuth2 (Web application).
You use a computer with a browser (to get the token) and a Debian server (Odroid, Raspberry Pi, etc.) where the app runs. Follow the steps in order.
Step 1. Get credentials.json from Google Cloud
Step 2. Install Python 3.11+ and pipx
Step 3. Install fetch2gmail
pipx install fetch2gmailStep 4. Get the token
In the same folder where credentials.json is, run:
fetch2gmail authA browser opens. Sign in with the Gmail account that will receive the imported mail and click Allow. token.json is saved in that folder. Press Ctrl+C to stop the auth server.
Step 5. Copy these two files to the server
Copy credentials.json and token.json to your Debian server. You will put them in the app data directory in the next section.
Step 6. Create the data directory and add the files
Pick one directory for all app files (e.g. /opt/fetch2gmail or /home/odroid/fetch2gmail). Create it and put credentials.json and token.json there:
sudo mkdir -p /opt/fetch2gmail
sudo chown "$USER" /opt/fetch2gmail
# Copy credentials.json and token.json into /opt/fetch2gmail (from step 5)Use the same path in the steps below (replace /opt/fetch2gmail and the username if you use something else).
Step 7. Install pipx and fetch2gmail on the server
Run these from any directory (you do not need to be in /opt/fetch2gmail):
sudo apt install pipx
pipx ensurepath
# Log out and back in (or source ~/.bashrc) so ~/.local/bin is on PATH
pipx install fetch2gmailThe app will find your token and files in /opt/fetch2gmail because in step 9 you install a single systemd service that sets WorkingDirectory and FETCH2GMAIL_CONFIG to that directory. If you set the UI password from the CLI (step 8), cd into /opt/fetch2gmail first so .ui_auth is created there.
Step 8. Set the UI username and password
The web UI is protected by a username and password (stored as a hash in .ui_auth). You can set it from the CLI or from the UI:
Step 9. Install and enable the systemd service (one service: web UI + background fetch)
One service runs the web UI and polls your ISP mailbox on a schedule (every 5 minutes by default). Generate the unit file and install it:
fetch2gmail install-service --user YOUR_USER --dir /opt/fetch2gmail -o /tmp/fetch2gmail.service
sudo mv /tmp/fetch2gmail.service /etc/systemd/system/fetch2gmail.service
sudo systemctl daemon-reload
sudo systemctl enable fetch2gmail
sudo systemctl start fetch2gmailReplace YOUR_USER with the user that owns /opt/fetch2gmail (e.g. odroid). Replace /opt/fetch2gmail if you used a different data directory. The command finds fetch2gmail on your PATH; if it is elsewhere, add --exec /path/to/fetch2gmail.
Alternatively, print the unit file and pipe it into place:
fetch2gmail install-service --user YOUR_USER --dir /opt/fetch2gmail | sudo tee /etc/systemd/system/fetch2gmail.service > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable fetch2gmail
sudo systemctl start fetch2gmailStep 10. Configure ISP mail in the web UI
Open http://<server-ip>:8765 in your browser (e.g. http://192.168.1.10:8765). Log in with the UI username and password from step 8. You will see the initial setup form. Enter your IMAP host, username, password, mailbox, and Gmail label, then click Create config. The app stores your password securely and will poll your ISP mailbox on a schedule (every 5 minutes by default). You do not need to edit any config file by hand.
Done. To watch logs: journalctl -u fetch2gmail -f.
Everything runs on one machine (laptop or desktop) that has a browser. You get token.json via fetch2gmail auth (CLI); the web UI does not have "Sign in with Google". Then set a UI password and configure ISP mail in the UI. Follow the steps in order.
Step 1. Get credentials.json from Google Cloud
Same as Use case 1, step 1. Save credentials.json in a folder you will use as the app data directory (e.g. ~/fetch2gmail).
Step 2. Install Python 3.11+ and pipx
Same as Use case 1, step 2 (Windows or Linux).
Step 3. Install fetch2gmail
pipx install fetch2gmailStep 4. Create the data directory and add credentials
Create the folder and put credentials.json there (you do not have a token yet):
mkdir -p ~/fetch2gmail
# Copy credentials.json into ~/fetch2gmailUse the same path in the steps below (replace ~/fetch2gmail if you use something else).
Step 5. Get token.json (CLI)
cd into your data directory and run fetch2gmail auth. A browser opens; sign in with the Gmail account that will receive the imported mail and click Allow. token.json is saved in that folder. Press Ctrl+C to stop the auth server.
Step 6. Install and enable the systemd service
Same as Use case 1, step 9: one service (web UI + background fetch). Replace YOUR_USER and the path with yours:
fetch2gmail install-service --user YOUR_USER --dir /home/YOUR_USER/fetch2gmail | sudo tee /etc/systemd/system/fetch2gmail.service > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable fetch2gmail
sudo systemctl start fetch2gmailStep 7. Set UI password and configure ISP mail
Open http://127.0.0.1:8765 in your browser. If .ui_auth does not exist yet, you will see the Set UI password wizard; set a username and password there (or run fetch2gmail set-ui-password from the CLI). Then use the initial setup form to enter your IMAP host, username, password, mailbox, and Gmail label, and click Create config. The app stores your password securely and the timer runs the fetch on the schedule you set (every 5 minutes by default).
Done.
Your Google OAuth client must use Web application (not Desktop) and have these Authorized redirect URIs:
Google does not allow redirect URIs that use an IP address. That is why the token must be obtained on a machine where the app can use localhost (Use case 1 steps 1–4 on a PC; Use case 2 on the same machine).
The app runs the fetch every 5 minutes by default (background poller in the same process as the web UI). You can change the interval in the web UI (Config) or in config.json (poll_interval_minutes).
All app files live in one directory: config.json, credentials.json, token.json, and optionally .env (IMAP password; the UI can store it for you). The app creates state.db and .ui_auth there. Set WorkingDirectory and FETCH2GMAIL_CONFIG to this directory in systemd.
One instance = one IMAP mailbox → one Gmail account. To run multiple accounts (e.g. two ISP mailboxes forwarding to two Gmail addresses), run multiple instances, each with its own data directory and its own port so the UIs don’t conflict.
Second instance (example):
Create a second data directory and put config and credentials there (e.g. /opt/fetch2gmail2 with its own config.json, credentials.json, token.json, .env). Use the same GCP credentials.json if you like; get a separate token.json for the second Gmail account (run fetch2gmail auth and sign in with the second Gmail, then copy that token.json into the second directory).
Generate a second systemd unit with a different port and unit name:
fetch2gmail install-service --user YOUR_USER --dir /opt/fetch2gmail2 -o /tmp/fetch2gmail2.serviceInstall the second unit under a different name so both can run, then edit its ExecStart to add a port:
sudo mv /tmp/fetch2gmail2.service /etc/systemd/system/fetch2gmail2.service
sudo nano /etc/systemd/system/fetch2gmail2.serviceIn the editor, find the ExecStart= line and make sure it includes a port flag, for example:
ExecStart=/home/YOUR_USER/.local/pipx/venvs/fetch2gmail/bin/fetch2gmail serve --host 0.0.0.0 --port 8766
Also set WorkingDirectory and Environment=FETCH2GMAIL_CONFIG to the second directory (e.g. /opt/fetch2gmail2, /opt/fetch2gmail2/config.json). Then reload and enable:
sudo systemctl daemon-reload
sudo systemctl enable fetch2gmail2
sudo systemctl start fetch2gmail2Run fetch2gmail set-ui-password once from the second directory (or with FETCH2GMAIL_CONFIG set to the second config) so the second UI is protected.
Open the first UI at http://server:8765 and the second at http://server:8766. Each has its own config, token, and poller.
You need a different port per instance — each fetch2gmail serve binds to one port, so a second instance must use --port 8766 (or another free port).
"Background fetch skipped: IMAP password could not be decrypted" — The encrypted IMAP password in .env was created with a different encryption key (e.g. different config directory or .cookie_secret). Re-enter the IMAP password in the UI (Config section) and Save to re-encrypt it.
To see what the service is doing (background poller, fetch results, errors), stream the logs:
journalctl -u fetch2gmail -fYou’ll see messages like "Poller: next fetch in Xs", "Poller: running fetch now", and "Background fetch: imported=...". Use Ctrl+C to stop following. For a second instance (e.g. fetch2gmail2), use journalctl -u fetch2gmail2 -f.
| Command | Purpose |
|---|---|
| fetch2gmail --version | Show installed version. |
| fetch2gmail auth | Get token.json on a machine with a browser (opens http://127.0.0.1:8765). |
| fetch2gmail set-ui-password | Set UI username and password (hash in .ui_auth). |
| fetch2gmail install-service | Generate systemd unit file (one service: web UI + background fetch). Use --user, --dir, and optionally --output or pipe to sudo tee. |
| fetch2gmail serve | Run the web UI. By default uses the current directory for config, credentials, and token; use --port and --host as needed. See below. |
| fetch2gmail run | Run one fetch cycle. |
| fetch2gmail run --dry-run | Connect to ISP and show what would be imported; no Gmail import, no delete. |
fetch2gmail serve — defaults and options
To check your installed version: fetch2gmail --version.
If you installed with pipx, upgrade to the latest release with:
pipx upgrade fetch2gmailYou do not need to stop the service, remove the systemd unit, or uninstall first. The service will use the new version the next time it runs (e.g. after a reboot), or restart it now to pick up the update:
sudo systemctl restart fetch2gmailYour config, token, and data directory are unchanged.
Machine used only to get the token (no system service): After copying credentials.json and token.json to the server, remove the app: pipx uninstall fetch2gmail. Reinstall with pipx install fetch2gmail if you need to run fetch2gmail auth again later.
Machine where the app runs (system service + app): To remove everything so you can reinstall or run from source and see your changes:
Stop and disable the systemd service, then remove the unit file:
sudo systemctl stop fetch2gmail
sudo systemctl disable fetch2gmail
sudo rm /etc/systemd/system/fetch2gmail.service
sudo systemctl daemon-reload(To confirm the service is gone: systemctl status fetch2gmail should report "not found".)
Uninstall the app:
Optionally remove the data directory (config, credentials, token, state, UI password file):
rm -rf /opt/fetch2gmailUse the path you used as the data directory (e.g. /opt/fetch2gmail or ~/fetch2gmail). Only do this if you no longer need the config or token; back them up first if you might reuse them.
To test local changes: Uninstall the system copy (steps 1 and 2 above), then from your git clone run python3 -m venv .venv, source .venv/bin/activate, pip install -e .. Use that terminal to run fetch2gmail serve (or reinstall the system service later with fetch2gmail install-service and point it at your data directory).
To run from a git clone (e.g. to test changes before pushing):
Clone and go into the repo:
git clone https://github.com/yourusername/fetch2gmail.git
cd fetch2gmailCreate and activate a virtual environment:
Install the package in editable mode (so changes in the repo are used when you run the app):
pip install -e .Optional, for tests: pip install -e ".[dev]"
Run and test (use the same terminal with the venv active; no pipx needed—the editable install provides the fetch2gmail command):
Using a different data directory: If you want to run fetch2gmail serve from your git clone but use config and data from another folder (e.g. credentials.json and token.json on your Desktop), set FETCH2GMAIL_CONFIG to the full path to config.json in that folder (e.g. FETCH2GMAIL_CONFIG=/home/sarah/Desktop/config.json). config.json does not need to exist yet—the UI creates it when you complete the setup form.
pip install build
python -m buildMIT. See LICENSE.
| Back | FazBrowse Home | New Git URL |