Переглянути джерело

Dateien per Mail hochladen

gc-server3 2 тижнів тому
батько
коміт
2564bc7b75
3 змінених файлів з 67 додано та 0 видалено
  1. 55 0
      app/imap.py
  2. 1 0
      pyproject.toml
  3. 11 0
      uv.lock

+ 55 - 0
app/imap.py

@@ -0,0 +1,55 @@
+import re
+from dataclasses import dataclass
+from pathlib import Path
+
+from db import get_session
+from imap_tools import MailBox
+from sqlalchemy import text
+
+
+@dataclass
+class ImapCredentials:
+    server: str
+    username: str
+    password: str
+
+
+creds = ImapCredentials("mail.global-cube.com", "forderung", "Navision2013!")
+fileserver = Path("C:\\Projekte\\Reisacher-Fileserver")
+
+
+def main():
+    db = next(get_session())
+    q = db.execute(text("SELECT Client_DB, Document_No FROM dbo.Forderungen")).fetchall()
+    lookup = {row[1]: row[0] for row in q}
+
+    with MailBox(creds.server).login(creds.username, creds.password) as mb:
+        messages = mb.fetch(
+            mark_seen=True,
+            bulk=True,
+            limit=100,
+            headers_only=False,
+            reverse=True,
+        )
+
+        for msg in messages:
+            match = re.findall(r"([WV]\w+\d+)", msg.subject)
+            if not match:
+                print(msg.subject)
+                continue
+            print(match)
+            invoice_no = match[0]
+            if invoice_no not in lookup:
+                continue
+            client_db = lookup[invoice_no]
+            print(invoice_no, client_db)
+            attachments = {att.filename: att.payload for att in msg.attachments if att.size > 0}
+            for file, content in attachments.items():
+                filename: Path = fileserver / client_db / invoice_no / file
+                filename.parent.mkdir(exist_ok=True, parents=True)
+                filename.write_bytes(content)
+                print(filename)
+
+
+if __name__ == "__main__":
+    main()

+ 1 - 0
pyproject.toml

@@ -19,4 +19,5 @@ dependencies = [
 	"aiofiles>=23.1",
 	"python-dotenv>=1.2.1",
 	"docxtpl>=0.20.2",
+	"imap-tools>=1.12.1",
 ]

+ 11 - 0
uv.lock

@@ -72,6 +72,7 @@ dependencies = [
     { name = "alembic" },
     { name = "docxtpl" },
     { name = "fastapi", extra = ["standard"] },
+    { name = "imap-tools" },
     { name = "jinja2" },
     { name = "ldap3" },
     { name = "openpyxl" },
@@ -90,6 +91,7 @@ requires-dist = [
     { name = "alembic", specifier = ">=1.11" },
     { name = "docxtpl", specifier = ">=0.20.2" },
     { name = "fastapi", extras = ["standard"], specifier = ">=0.95" },
+    { name = "imap-tools", specifier = ">=1.12.1" },
     { name = "jinja2", specifier = ">=3.1" },
     { name = "ldap3", specifier = ">=2.9" },
     { name = "openpyxl", specifier = ">=3.1" },
@@ -399,6 +401,15 @@ wheels = [
     { url = "https://files.pythonhosted.org/packages/0e/61/66938bbb5fc52dbdf84594873d5b51fb1f7c7794e9c0f5bd885f30bc507b/idna-3.11-py3-none-any.whl", hash = "sha256:771a87f49d9defaf64091e6e6fe9c18d4833f140bd19464795bc32d966ca37ea", size = 71008, upload-time = "2025-10-12T14:55:18.883Z" },
 ]
 
+[[package]]
+name = "imap-tools"
+version = "1.12.1"
+source = { registry = "https://pypi.org/simple" }
+sdist = { url = "https://files.pythonhosted.org/packages/a0/f1/0cfb0496d141a2fd960bb512d462c9e0bbc10c99d75210e303cc3938f7e6/imap_tools-1.12.1.tar.gz", hash = "sha256:f70169e4efcf2fafcc9fc48b391e9a69b6f40c45faca92f2723b991194906ac9", size = 47492, upload-time = "2026-04-20T06:44:46.194Z" }
+wheels = [
+    { url = "https://files.pythonhosted.org/packages/4b/5e/982243b6b2a52c09cd0006c5c014cae27e36bf36215b9541b9c0d235b5fc/imap_tools-1.12.1-py3-none-any.whl", hash = "sha256:ed73cdfe266b09c9b6c3d5a28fb0b8608566c20def10fec18760c315dccb9b6e", size = 35722, upload-time = "2026-04-20T06:44:44.491Z" },
+]
+
 [[package]]
 name = "jinja2"
 version = "3.1.6"