diff --git a/tools/email_forward.py b/tools/email_forward.py index f7a46f9..c3c44b4 100644 --- a/tools/email_forward.py +++ b/tools/email_forward.py @@ -17,7 +17,8 @@ sys.path.insert(0, str(PROJECT_ROOT)) from tools.email_process import ( 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 import imaplib @@ -46,7 +47,7 @@ def extract_original_sender(body: str, from_full: str) -> tuple[str, str]: 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.""" # Extrage expeditorul original dacă e forward 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 = 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 if len(full) <= MAX_WA_LENGTH: @@ -138,6 +143,7 @@ def fetch_unread_emails(): 'from_full': from_addr, 'date': msg['Date'], 'body': get_email_body(msg), + 'attachments': get_email_attachments(msg), }) mail.logout() @@ -159,7 +165,7 @@ def run_forward(): for em in emails: subject = em['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: for i, part in enumerate(parts): diff --git a/tools/email_process.py b/tools/email_process.py index b0e72ab..d8757b0 100755 --- a/tools/email_process.py +++ b/tools/email_process.py @@ -80,6 +80,19 @@ def get_email_body(msg): body = payload.decode(charset, errors='replace') 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: """Extract just the email address from From header""" match = re.search(r'<([^>]+)>', from_header) @@ -137,7 +150,8 @@ def save_email_as_note(eid: str) -> dict: subject = decode_mime_header(msg['Subject']) date_str = msg['Date'] body = get_email_body(msg) - + attachments = get_email_attachments(msg) + # Check whitelist if sender_email not in WHITELIST: mail.logout() @@ -162,6 +176,12 @@ def save_email_as_note(eid: str) -> dict: filename = f"{date_prefix}_{slug}.md" 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 content = f"""# {subject} @@ -172,7 +192,7 @@ def save_email_as_note(eid: str) -> dict: --- {body} - +{attachments_section} --- ## TL;DR