diff --git a/app/stiftung/tasks.py b/app/stiftung/tasks.py index bcf7a1d..4a9d2c6 100644 --- a/app/stiftung/tasks.py +++ b/app/stiftung/tasks.py @@ -137,7 +137,7 @@ def _upload_to_paperless(content: bytes, filename: str, destinataer=None, betref headers=headers, data=form_data, files=files, - timeout=60, + timeout=300, # 5 Minuten für große Anhänge ) response.raise_for_status() # Paperless gibt die neue Dokument-ID zurück (als Integer oder UUID-String) @@ -196,11 +196,12 @@ def poll_destinataer_emails(self, search_all_recent_days=0): errors = 0 try: - # IMAP-Verbindung aufbauen + # IMAP-Verbindung aufbauen (mit Socket-Timeout für große E-Mails) + imap_timeout = 120 # Sekunden – genug für große Anhänge if imap_use_ssl: - mail = imaplib.IMAP4_SSL(imap_host, imap_port) + mail = imaplib.IMAP4_SSL(imap_host, imap_port, timeout=imap_timeout) else: - mail = imaplib.IMAP4(imap_host, imap_port) + mail = imaplib.IMAP4(imap_host, imap_port, timeout=imap_timeout) mail.login(imap_user, imap_password) mail.select(imap_folder) @@ -279,6 +280,10 @@ def poll_destinataer_emails(self, search_all_recent_days=0): filename = _decode_header_value(part.get_filename() or "") content = part.get_payload(decode=True) if not content: + logger.warning( + "Anhang '%s' hat keinen Inhalt (möglicherweise zu groß oder beschädigt) – wird übersprungen.", + filename, + ) continue doc_id = _upload_to_paperless( diff --git a/app/stiftung/views/geschichte.py b/app/stiftung/views/geschichte.py index 1f50483..158431e 100644 --- a/app/stiftung/views/geschichte.py +++ b/app/stiftung/views/geschichte.py @@ -695,14 +695,22 @@ def email_eingang_poll_trigger(request): try: # Synchron ausführen für sofortiges Feedback; sucht auch bereits # gelesene E-Mails der letzten 30 Tage (Duplikate werden übersprungen). - result = poll_destinataer_emails.apply(kwargs={"search_all_recent_days": 30}).get(timeout=60) + result = poll_destinataer_emails.apply(kwargs={"search_all_recent_days": 30}).get(timeout=300) processed = result.get("processed", 0) if isinstance(result, dict) else 0 if result and result.get("status") == "skipped": messages.warning(request, "IMAP ist nicht konfiguriert. Bitte Einstellungen unter Administration → E-Mail / IMAP prüfen.") elif processed > 0: - messages.success(request, f"{processed} neue E-Mail(s) importiert.") + error_count = result.get("errors", 0) if isinstance(result, dict) else 0 + if error_count > 0: + messages.warning(request, f"{processed} E-Mail(s) importiert, aber {error_count} Fehler aufgetreten. Bitte Logs prüfen.") + else: + messages.success(request, f"{processed} neue E-Mail(s) importiert.") else: - messages.info(request, "Keine neuen E-Mails gefunden.") + error_count = result.get("errors", 0) if isinstance(result, dict) else 0 + if error_count > 0: + messages.warning(request, f"Keine neuen E-Mails importiert, aber {error_count} Fehler aufgetreten. Bitte Logs prüfen.") + else: + messages.info(request, "Keine neuen E-Mails gefunden.") except Exception as exc: messages.error(request, f"Fehler beim E-Mail-Abruf: {exc}") return redirect("stiftung:email_eingang_list")