feat(email): show attachments in digest and forward commands

Add get_email_attachments() helper that extracts filenames from MIME
parts. Email notes now include an Atașamente section; forwarded emails
show attachment names in the WhatsApp header.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-22 07:41:21 +00:00
parent eb693a2e71
commit 56f6c0df01
2 changed files with 32 additions and 6 deletions

View File

@@ -17,7 +17,8 @@ sys.path.insert(0, str(PROJECT_ROOT))
from tools.email_process import ( from tools.email_process import (
IMAP_SERVER, IMAP_PORT, IMAP_USER, IMAP_PASS, IMAP_SERVER, IMAP_PORT, IMAP_USER, IMAP_PASS,
WHITELIST, decode_mime_header, extract_sender_email, get_email_body WHITELIST, decode_mime_header, extract_sender_email, get_email_body,
get_email_attachments
) )
from src.config import Config from src.config import Config
import imaplib import imaplib
@@ -46,7 +47,7 @@ def extract_original_sender(body: str, from_full: str) -> tuple[str, str]:
return from_full, "" return from_full, ""
def format_for_whatsapp(subject: str, from_full: str, date: str, body: str) -> str: def format_for_whatsapp(subject: str, from_full: str, date: str, body: str, attachments: list = None) -> str:
"""Curăță corpul emailului și îl formatează pentru WhatsApp.""" """Curăță corpul emailului și îl formatează pentru WhatsApp."""
# Extrage expeditorul original dacă e forward # Extrage expeditorul original dacă e forward
original_from, original_date = extract_original_sender(body, from_full) original_from, original_date = extract_original_sender(body, from_full)
@@ -70,7 +71,11 @@ def format_for_whatsapp(subject: str, from_full: str, date: str, body: str) -> s
body = '\n'.join(line.rstrip() for line in body.splitlines()) body = '\n'.join(line.rstrip() for line in body.splitlines())
body = body.strip() body = body.strip()
header = f"*{subject}*\nDe la: {display_from}\nPrimit: {display_date}\n---\n" att_line = ""
if attachments:
att_line = "\nAtașamente: " + ", ".join(attachments) + "\n"
header = f"*{subject}*\nDe la: {display_from}\nPrimit: {display_date}{att_line}\n---\n"
full = header + body full = header + body
if len(full) <= MAX_WA_LENGTH: if len(full) <= MAX_WA_LENGTH:
@@ -138,6 +143,7 @@ def fetch_unread_emails():
'from_full': from_addr, 'from_full': from_addr,
'date': msg['Date'], 'date': msg['Date'],
'body': get_email_body(msg), 'body': get_email_body(msg),
'attachments': get_email_attachments(msg),
}) })
mail.logout() mail.logout()
@@ -159,7 +165,7 @@ def run_forward():
for em in emails: for em in emails:
subject = em['subject'] subject = em['subject']
print(f"Trimit: {subject}") print(f"Trimit: {subject}")
parts = format_for_whatsapp(subject, em['from_full'], em['date'], em['body']) parts = format_for_whatsapp(subject, em['from_full'], em['date'], em['body'], em.get('attachments', []))
if DRY_RUN: if DRY_RUN:
for i, part in enumerate(parts): for i, part in enumerate(parts):

View File

@@ -80,6 +80,19 @@ def get_email_body(msg):
body = payload.decode(charset, errors='replace') body = payload.decode(charset, errors='replace')
return body.strip() return body.strip()
def get_email_attachments(msg) -> list:
"""Extract list of attachment filenames from email MIME parts."""
attachments = []
if not msg.is_multipart():
return attachments
for part in msg.walk():
filename = part.get_filename()
if filename:
attachments.append(decode_mime_header(filename))
elif part.get('Content-Disposition', '').lower().startswith('attachment'):
attachments.append(f"[{part.get_content_type()}]")
return attachments
def extract_sender_email(from_header: str) -> str: def extract_sender_email(from_header: str) -> str:
"""Extract just the email address from From header""" """Extract just the email address from From header"""
match = re.search(r'<([^>]+)>', from_header) match = re.search(r'<([^>]+)>', from_header)
@@ -137,6 +150,7 @@ def save_email_as_note(eid: str) -> dict:
subject = decode_mime_header(msg['Subject']) subject = decode_mime_header(msg['Subject'])
date_str = msg['Date'] date_str = msg['Date']
body = get_email_body(msg) body = get_email_body(msg)
attachments = get_email_attachments(msg)
# Check whitelist # Check whitelist
if sender_email not in WHITELIST: if sender_email not in WHITELIST:
@@ -162,6 +176,12 @@ def save_email_as_note(eid: str) -> dict:
filename = f"{date_prefix}_{slug}.md" filename = f"{date_prefix}_{slug}.md"
filepath = KB_PATH / filename filepath = KB_PATH / filename
# Build attachments section
attachments_section = ""
if attachments:
att_list = "\n".join(f"- {a}" for a in attachments)
attachments_section = f"\n## Atașamente\n{att_list}\n"
# Create markdown note # Create markdown note
content = f"""# {subject} content = f"""# {subject}
@@ -172,7 +192,7 @@ def save_email_as_note(eid: str) -> dict:
--- ---
{body} {body}
{attachments_section}
--- ---
## TL;DR ## TL;DR