Add NAS daily email report wrapper
Prompts used since the previous commit: - Hoe regelen we het dagrapport? - Het rapport moet per email naar mij gestuurd worden. - hans@australius.nl - akkoord Special observations: - dagelijks_overzicht.py now supports --mail-to and sends zero-message reports. - QNAP plain deployment installs run_daily_report.sh with DAILY_REPORT_TO defaulting to hans@australius.nl. - verplaats_log.json remains modified from the user's run and was intentionally not staged.
This commit is contained in:
+20
-10
@@ -7,7 +7,7 @@ berichten die gisteren zijn binnengekomen, en stuurt een HTML-samenvatting
|
||||
naar hans@australius.nl.
|
||||
|
||||
Eenmalige setup: python3 dagelijks_overzicht.py --setup
|
||||
Handmatig testen: python3 dagelijks_overzicht.py [--gisteren | --datum 2026-07-01]
|
||||
Handmatig testen: python3 dagelijks_overzicht.py [--gisteren | --datum 2026-07-01] [--mail-to adres]
|
||||
Plannen (macOS): zie dagelijks_overzicht.plist
|
||||
"""
|
||||
|
||||
@@ -27,6 +27,7 @@ from collections import defaultdict
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from imap_utils import list_folders, quote_mailbox
|
||||
from mail_imap_ops import fetch_body
|
||||
|
||||
# Mappen die NIET in het overzicht komen (systeem + inbox zelf)
|
||||
UITSLUITINGEN = {
|
||||
@@ -59,6 +60,17 @@ def load_config():
|
||||
}
|
||||
|
||||
|
||||
def arg_value(args: list[str], flag: str) -> str | None:
|
||||
"""Return the value after a simple command-line flag."""
|
||||
if flag not in args:
|
||||
return None
|
||||
idx = args.index(flag)
|
||||
if idx + 1 >= len(args):
|
||||
print(f"Ontbrekende waarde voor {flag}")
|
||||
sys.exit(2)
|
||||
return args[idx + 1]
|
||||
|
||||
|
||||
# ── helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
def decode_hdr(value) -> str:
|
||||
@@ -128,10 +140,11 @@ def haal_berichten_op(mail: imaplib.IMAP4_SSL, zoekdatum: date) -> dict[str, lis
|
||||
status, msg_data = mail.fetch(
|
||||
uid, "(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE)])"
|
||||
)
|
||||
if status != "OK" or not msg_data or not msg_data[0]:
|
||||
raw_header = fetch_body(msg_data)
|
||||
if status != "OK" or raw_header is None:
|
||||
continue
|
||||
|
||||
msg = email.message_from_bytes(msg_data[0][1])
|
||||
msg = email.message_from_bytes(raw_header)
|
||||
from_raw = decode_hdr(msg.get("From", ""))
|
||||
subject = decode_hdr(msg.get("Subject", "(geen onderwerp)"))
|
||||
date_str = msg.get("Date", "")
|
||||
@@ -232,12 +245,13 @@ def stuur_overzicht(html: str, zoekdatum: date, config: dict):
|
||||
def run():
|
||||
args = sys.argv[1:]
|
||||
cfg_dict = load_config()
|
||||
if mail_to := arg_value(args, "--mail-to"):
|
||||
cfg_dict["to"] = mail_to
|
||||
print(f"Account: {cfg_dict['username']}\n")
|
||||
|
||||
# Datum bepalen
|
||||
if "--datum" in args:
|
||||
idx = args.index("--datum")
|
||||
zoekdatum = date.fromisoformat(args[idx + 1])
|
||||
if datum := arg_value(args, "--datum"):
|
||||
zoekdatum = date.fromisoformat(datum)
|
||||
else:
|
||||
zoekdatum = date.today() - timedelta(days=1) # standaard: gisteren
|
||||
|
||||
@@ -256,10 +270,6 @@ def run():
|
||||
totaal = sum(len(v) for v in berichten.values())
|
||||
print(f"{totaal} berichten gevonden in {len(berichten)} mappen.")
|
||||
|
||||
if totaal == 0:
|
||||
print("Niets te sturen.")
|
||||
return
|
||||
|
||||
html = bouw_html(berichten, zoekdatum)
|
||||
stuur_overzicht(html, zoekdatum, cfg_dict)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user