Skip to content

Commit 2a1c13f

Browse files
committed
fix(scripts): address review feedback on IMAP email reader
- Remove bundled pipeline agent configs (scope creep + security issues) - Add EMAIL_USERNAME validation (was missing, causes confusing IMAP error) - Add IMAP_PORT error handling for non-integer values - Move `import re` to module top level - Add docstring documenting env vars and non-Gmail behavior
1 parent 89ba141 commit 2a1c13f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

scripts/read_email.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
#!/usr/bin/env python3
2-
"""Read latest email from a given sender via IMAP, searching multiple folders."""
2+
"""Read latest email from a given sender via IMAP, searching multiple folders.
3+
4+
Environment variables:
5+
EMAIL_USERNAME - IMAP login username (required)
6+
EMAIL_PASSWORD - IMAP login password (required)
7+
IMAP_SERVER - IMAP server hostname (default: imap.gmail.com)
8+
IMAP_PORT - IMAP server port (default: 993)
9+
10+
Non-Gmail servers will only match INBOX, Promotions, and Updates folders;
11+
Gmail-specific folders like [Gmail]/All Mail are skipped gracefully.
12+
"""
313
import imaplib
414
import email
515
import os
16+
import re
617
import sys
718
from email.header import decode_header
819

920
IMAP_SERVER = os.environ.get("IMAP_SERVER", "imap.gmail.com")
10-
IMAP_PORT = int(os.environ.get("IMAP_PORT", "993"))
21+
try:
22+
IMAP_PORT = int(os.environ.get("IMAP_PORT", "993"))
23+
except ValueError:
24+
print("ERROR: IMAP_PORT must be an integer", file=sys.stderr)
25+
sys.exit(1)
1126

1227

1328
def decode_str(s, enc=None):
@@ -32,7 +47,6 @@ def get_body(msg):
3247
if ct == "text/html" and "attachment" not in cd:
3348
payload = part.get_payload(decode=True)
3449
if payload:
35-
import re
3650
text = payload.decode("utf-8", errors="ignore")
3751
text = re.sub(r'<[^>]+>', ' ', text)
3852
text = re.sub(r'\s+', ' ', text).strip()
@@ -64,8 +78,12 @@ def main():
6478
password = os.environ.get("EMAIL_PASSWORD", "")
6579
username = os.environ.get("EMAIL_USERNAME", "")
6680

81+
if not username:
82+
print("ERROR: EMAIL_USERNAME not set", file=sys.stderr)
83+
sys.exit(1)
84+
6785
if not password:
68-
print("ERROR: EMAIL_PASSWORD not set")
86+
print("ERROR: EMAIL_PASSWORD not set", file=sys.stderr)
6987
sys.exit(1)
7088

7189
try:

0 commit comments

Comments
 (0)