parse EMail
This commit is contained in:
parent
7413b54af4
commit
726d2607da
|
|
@ -3,7 +3,7 @@ services:
|
|||
container_name: email-api
|
||||
image: python:3.12-slim
|
||||
restart: unless-stopped
|
||||
network_mode: host # Nutzt das Host-Netzwerk
|
||||
network_mode: host
|
||||
volumes:
|
||||
- ./email_api:/app
|
||||
working_dir: /app
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import gzip
|
|||
import logging
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from email.parser import BytesParser
|
||||
from email.policy import default
|
||||
|
||||
# Python-Version prüfen
|
||||
if sys.version_info < (3, 12):
|
||||
|
|
@ -19,7 +21,7 @@ logging.basicConfig(level=logging.INFO)
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Konfiguration
|
||||
SMTP_HOST = "localhost" # Verweist auf Host-Port 25
|
||||
SMTP_HOST = "localhost" # Host-Netzwerkmodus
|
||||
SMTP_PORT = 25 # Fest auf Port 25 ohne TLS
|
||||
API_TOKEN = os.environ.get('API_TOKEN')
|
||||
if not API_TOKEN:
|
||||
|
|
@ -44,14 +46,25 @@ def process_email(domain):
|
|||
logger.info(f"[{request_id}] Processing email for domain: {domain}")
|
||||
|
||||
try:
|
||||
# Entkomprimieren, falls komprimiert
|
||||
if compressed:
|
||||
email_bytes = base64.b64decode(email_content)
|
||||
email_content = gzip.decompress(email_bytes).decode('utf-8')
|
||||
email_content = gzip.decompress(email_bytes)
|
||||
else:
|
||||
email_content = base64.b64decode(email_content).decode('utf-8')
|
||||
email_content = base64.b64decode(email_content)
|
||||
|
||||
# E-Mail-Header parsen
|
||||
email_msg = BytesParser(policy=default).parsebytes(email_content)
|
||||
from_addr = email_msg['From'] or 'lambda@andreasknuth.de' # Fallback
|
||||
to_addr = email_msg['To']
|
||||
if not to_addr:
|
||||
raise ValueError("Kein Empfänger (To) in der E-Mail gefunden")
|
||||
|
||||
logger.info(f"[{request_id}] From: {from_addr}, To: {to_addr}")
|
||||
|
||||
# An Postfix weiterleiten
|
||||
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as smtp:
|
||||
smtp.sendmail('lambda@andreasknuth.de', f'inbox@{domain}', email_content)
|
||||
smtp.sendmail(from_addr, to_addr, email_content)
|
||||
logger.info(f"[{request_id}] Email forwarded to Postfix for {domain}")
|
||||
|
||||
return jsonify({'message': 'Email processed', 'request_id': request_id}), 200
|
||||
|
|
|
|||
Loading…
Reference in New Issue