Scripts and patterns for reading multiple Gmail (or IMAP) accounts without a GUI.
App Passwords are 16-character passwords that work even with 2FA enabled. They are not your account password.
- Go to myaccount.google.com/apppasswords
- Sign in to the target Google account
- Under "Select app", choose "Mail" (or type a custom name)
- Under "Select device", choose "Other" and name it (e.g. "server-imap")
- Click "Generate"
- Copy the 16-character password immediately (it is shown only once)
- Use this password as
IMAP_PASS_*in your.env
Repeat for each account.
Always use BODY.PEEK[] instead of RFC822 when fetching message bodies.
# CORRECT - does not mark as read
status, data = imap.fetch(num, '(BODY.PEEK[])')
# WRONG - marks message as read
status, data = imap.fetch(num, '(RFC822)')If you accidentally mark a message as read:
imap.store(num, '-FLAGS', '\\Seen')Copy .env.example to .env and fill in your accounts:
cp .env.example .envAccounts are numbered sequentially:
IMAP_LABEL_1=personal
IMAP_USER_1=you@gmail.com
IMAP_PASS_1=abcd efgh ijkl mnop
IMAP_SERVER_1=imap.gmail.com
IMAP_LABEL_2=work
IMAP_USER_2=you@yourcompany.com
IMAP_PASS_2=xxxx xxxx xxxx xxxx
IMAP_SERVER_2=imap.gmail.com
The script reads as many IMAP_LABEL_N / IMAP_USER_N / IMAP_PASS_N / IMAP_SERVER_N groups as it finds.
# List unread emails across all accounts
python3 email-check.py
# Filter to one account
python3 email-check.py --account personal
# Search by keyword in subject or body snippet
python3 email-check.py --search "invoice"
# Filter by sender
python3 email-check.py --from "billing@stripe.com"
# Combine filters
python3 email-check.py --account work --search "deploy"- Store
.envwith restricted permissions:chmod 600 .env - Never commit
.envto version control (add to.gitignore) - App passwords can be revoked individually at myaccount.google.com/apppasswords without changing your main password
- For non-Gmail IMAP servers, set
IMAP_SERVER_Nto the correct IMAP hostname (e.g.imap.mail.yahoo.com) - Gmail requires IMAP to be enabled: Gmail Settings -> See all settings -> Forwarding and POP/IMAP -> Enable IMAP
Yahoo also uses App Passwords. Generate one at login.yahoo.com/security/app-passwords/new and set IMAP_SERVER_N=imap.mail.yahoo.com.