Add Apple Mail links to daily report

The prompts used to arrive at this change since the previous commit

User asked whether daily report emails could include links, then chose Apple Mail links as the first implementation.

Any special observations that may be relevant for version management for this version. Be brief.

Apple Mail links use Message-ID-based message:// URLs and depend on macOS Mail having the messages synced/indexed locally. verplaats_log.json and plan.md were left unstaged.
This commit is contained in:
2026-07-05 11:22:03 +02:00
parent ae0813d519
commit 99cd891b1e
3 changed files with 82 additions and 14 deletions
+29 -7
View File
@@ -20,6 +20,8 @@ import sys
import ssl
import smtplib
import re
import html as html_lib
import urllib.parse
import config
from datetime import date, timedelta, datetime
from pathlib import Path
@@ -99,6 +101,16 @@ def leesbare_map(imap_naam: str) -> str:
return imap_naam.removeprefix("INBOX.").replace(".", " ")
def apple_mail_url(message_id: str) -> str:
"""Return an Apple Mail message:// URL for a Message-ID header."""
clean = (message_id or "").strip()
if not clean:
return ""
if not (clean.startswith("<") and clean.endswith(">")):
clean = f"<{clean.strip('<>')}>"
return "message://" + urllib.parse.quote(clean, safe="")
# ── IMAP ophalen ──────────────────────────────────────────────────────────────
def haal_berichten_op(mail: imaplib.IMAP4_SSL, zoekdatum: date) -> dict[str, list[dict]]:
@@ -138,7 +150,7 @@ def haal_berichten_op(mail: imaplib.IMAP4_SSL, zoekdatum: date) -> dict[str, lis
for uid in ids:
status, msg_data = mail.fetch(
uid, "(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE)])"
uid, "(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE MESSAGE-ID)])"
)
raw_header = fetch_body(msg_data)
if status != "OK" or raw_header is None:
@@ -148,6 +160,7 @@ def haal_berichten_op(mail: imaplib.IMAP4_SSL, zoekdatum: date) -> dict[str, lis
from_raw = decode_hdr(msg.get("From", ""))
subject = decode_hdr(msg.get("Subject", "(geen onderwerp)"))
date_str = msg.get("Date", "")
message_id = (msg.get("Message-ID") or "").strip()
# Tijdstip opmaken
try:
@@ -161,9 +174,10 @@ def haal_berichten_op(mail: imaplib.IMAP4_SSL, zoekdatum: date) -> dict[str, lis
afzender = m.group(1).strip() if m else from_raw.split("@")[0]
resultaat[folder].append({
"from": afzender[:40],
"from": afzender[:40],
"subject": subject[:80],
"time": tijdstip,
"time": tijdstip,
"message_id": message_id,
})
return resultaat
@@ -203,15 +217,23 @@ def bouw_html(berichten: dict[str, list[dict]], zoekdatum: date) -> str:
for folder in sorted(berichten.keys()):
items = berichten[folder]
label = leesbare_map(folder)
label = html_lib.escape(leesbare_map(folder))
html += f'<h2>{label} <span style="font-weight:normal;color:#888">({len(items)})</span></h2>\n'
html += '<table>\n'
for item in items:
time = html_lib.escape(item["time"])
sender = html_lib.escape(item["from"])
subject = html_lib.escape(item["subject"])
url = apple_mail_url(item.get("message_id", ""))
if url:
subject_html = f'<a href="{html_lib.escape(url, quote=True)}">{subject}</a>'
else:
subject_html = subject
html += (
f'<tr>'
f'<td class="time">{item["time"]}</td>'
f'<td class="from">{item["from"]}</td>'
f'<td class="subject">{item["subject"]}</td>'
f'<td class="time">{time}</td>'
f'<td class="from">{sender}</td>'
f'<td class="subject">{subject_html}</td>'
f'</tr>\n'
)
html += '</table>\n'