diff --git a/ses-lambda-python/lambda_function.py b/ses-lambda-python/lambda_function.py index f18f3e6..77bd50b 100644 --- a/ses-lambda-python/lambda_function.py +++ b/ses-lambda-python/lambda_function.py @@ -18,6 +18,9 @@ MAILCOW_API_KEY = os.environ.get('MAILCOW_API_KEY') def domain_to_bucket(domain): return domain.replace('.', '-') + '-emails' +def bucket_to_domain(bucket): + return bucket.replace('-emails', '').replace('-', '.') + def get_valid_inboxes(): url = 'https://mail.email-srvr.com/api/v1/get/mailbox/all' headers = {'X-API-Key': MAILCOW_API_KEY} @@ -69,7 +72,6 @@ def lambda_handler(event, context): } except Exception as e: print(f"Fehler beim Prüfen der Metadaten: {e}") - # Fortfahren, falls Metadaten nicht lesbar sind # Raw-Mail aus S3 holen resp = s3.get_object(Bucket=bucket, Key=key) @@ -90,6 +92,11 @@ def lambda_handler(event, context): recipients = to_addrs + cc_addrs + bcc_addrs print("Empfänger aus Headern:", recipients) + # Im S3-Flow nur Empfänger mit passender Domain behalten + expected_domain = bucket_to_domain(bucket) + recipients = [rcpt for rcpt in recipients if rcpt.lower().split('@')[1] == expected_domain] + print(f"Empfänger nach Domain-Filter ({expected_domain}): {recipients}") + if not recipients: raise Exception("Keine Empfänger gefunden, Abbruch") @@ -137,7 +144,7 @@ def lambda_handler(event, context): print("Metadatum 'processed:true' hinzugefügt.") except Exception as e: print(f"Fehler beim Schreiben des Metadatums: {e}") - raise # Fehler weiterleiten, um in CloudWatch sichtbar zu bleiben + raise return { 'statusCode': 200,