added amazon importer and logging smtp

This commit is contained in:
2026-03-20 16:22:38 +01:00
parent 9fdada5dbe
commit a4e39332c7
16 changed files with 2619 additions and 255 deletions
+12 -6
View File
@@ -11,8 +11,8 @@ from pypdf import PdfReader, PdfWriter
import qrcode
from qrcode.constants import ERROR_CORRECT_H
from app.database import get_settings, add_log_entry
from app.mail_processor import _connect_smtp, _build_forward_email
from app.database import get_settings, add_log_entry, get_import_email
from app.mail_processor import _connect_smtp, _build_forward_email, _send_with_log
logger = logging.getLogger(__name__)
@@ -84,11 +84,12 @@ def split_pdf(pdf_path: str, separator_pages: list[int]) -> list[bytes]:
return documents
async def process_scanned_pdf(pdf_path: str, progress_callback=None) -> dict:
async def process_scanned_pdf(pdf_path: str, progress_callback=None, beleg_type: str = "eingang") -> dict:
"""Full pipeline: detect separators, split, send each document via email."""
settings = await get_settings()
if not settings.get("smtp_server") or not settings.get("import_email"):
import_email = get_import_email(settings, beleg_type)
if not settings.get("smtp_server") or not import_email:
return {"error": "SMTP oder Import-Email nicht konfiguriert", "total_pages": 0, "documents": 0, "sent": 0, "errors": 1}
# Step 1: Detect separator pages (CPU-bound, run in thread)
@@ -135,12 +136,12 @@ async def process_scanned_pdf(pdf_path: str, progress_callback=None) -> dict:
filename = f"Scan_Dokument_{i + 1}.pdf"
msg = _build_forward_email(
from_addr=settings["smtp_username"],
to_addr=settings["import_email"],
to_addr=import_email,
original_subject=f"Scan-Upload Dokument {i + 1}/{len(documents)}",
original_from="Scan-Upload",
attachments=[(filename, doc_bytes)],
)
smtp_conn.send_message(msg)
smtp_log = _send_with_log(smtp_conn, msg)
sent += 1
await add_log_entry(
@@ -148,6 +149,9 @@ async def process_scanned_pdf(pdf_path: str, progress_callback=None) -> dict:
email_from="Scan-Upload",
attachments_count=1,
status="success",
sent_to=import_email,
smtp_log=smtp_log,
beleg_type=beleg_type,
)
except Exception as e:
@@ -159,6 +163,8 @@ async def process_scanned_pdf(pdf_path: str, progress_callback=None) -> dict:
attachments_count=1,
status="error",
error_message=str(e),
sent_to=import_email,
beleg_type=beleg_type,
)
except Exception as e: