feat(email): send attachments as WhatsApp documents, fix forward sender
- Add /send-document endpoint to WhatsApp bridge (base64 document send) - save_email_as_note() now saves attachment files to disk alongside note - email_digest: extract original sender for Fwd: emails so header shows the real author, not the forwarder; send attachment files after summary - email_forward: send attachment files as documents after text parts - Add extract_original_sender() and save_email_attachment_files() helpers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,8 @@ Usage:
|
||||
|
||||
import sys
|
||||
import re
|
||||
import base64
|
||||
import mimetypes
|
||||
import requests
|
||||
from pathlib import Path
|
||||
|
||||
@@ -115,6 +117,21 @@ def send_whatsapp(to: str, text: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def send_whatsapp_document(to: str, filename: str, data: bytes) -> bool:
|
||||
try:
|
||||
mimetype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
|
||||
resp = requests.post(
|
||||
f"{BRIDGE_URL}/send-document",
|
||||
json={"to": to, "filename": filename, "mimetype": mimetype,
|
||||
"data_base64": base64.b64encode(data).decode()},
|
||||
timeout=30,
|
||||
)
|
||||
return resp.json().get("ok", False)
|
||||
except Exception as e:
|
||||
print(f"[eroare send-document] {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def fetch_unread_emails():
|
||||
"""Preia emailurile necitite din inbox fără a le salva sau marca ca citite."""
|
||||
mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
|
||||
@@ -138,12 +155,24 @@ def fetch_unread_emails():
|
||||
if sender_email not in WHITELIST:
|
||||
continue
|
||||
|
||||
# Extract attachment data (name → bytes)
|
||||
att_data = {}
|
||||
if msg.is_multipart():
|
||||
for part in msg.walk():
|
||||
fname = part.get_filename()
|
||||
if fname:
|
||||
fname = decode_mime_header(fname)
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
att_data[fname] = payload
|
||||
|
||||
results.append({
|
||||
'subject': decode_mime_header(msg['Subject']),
|
||||
'from_full': from_addr,
|
||||
'date': msg['Date'],
|
||||
'body': get_email_body(msg),
|
||||
'attachments': get_email_attachments(msg),
|
||||
'attachments': list(att_data.keys()),
|
||||
'attachment_data': att_data,
|
||||
})
|
||||
|
||||
mail.logout()
|
||||
@@ -172,6 +201,8 @@ def run_forward():
|
||||
print(f"\n--- FORWARD {i+1}/{len(parts)} (dry-run) ---")
|
||||
print(part)
|
||||
print("------------------------\n")
|
||||
if em.get('attachment_data'):
|
||||
print(f"Atașamente: {list(em['attachment_data'].keys())}")
|
||||
else:
|
||||
for part in parts:
|
||||
ok = send_whatsapp(owner_jid, part)
|
||||
@@ -181,6 +212,13 @@ def run_forward():
|
||||
else:
|
||||
print(f"Trimis pe WhatsApp ({len(parts)} mesaje): {subject}")
|
||||
|
||||
for fname, fdata in em.get('attachment_data', {}).items():
|
||||
ok_att = send_whatsapp_document(owner_jid, fname, fdata)
|
||||
if ok_att:
|
||||
print(f"Atașament trimis: {fname}")
|
||||
else:
|
||||
print(f"Atașament eșuat: {fname}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_forward()
|
||||
|
||||
Reference in New Issue
Block a user