update acc. to bounces
This commit is contained in:
@@ -1,74 +1,97 @@
|
||||
import json
|
||||
import boto3
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
dynamo = boto3.resource('dynamodb', region_name='us-east-2')
|
||||
table = dynamo.Table('ses-outbound-messages')
|
||||
# AWS Clients
|
||||
s3 = boto3.client('s3')
|
||||
sqs = boto3.client('sqs')
|
||||
dynamodb = boto3.resource('dynamodb')
|
||||
|
||||
# DynamoDB Table
|
||||
OUTBOUND_TABLE = os.environ.get('OUTBOUND_TABLE', 'ses-outbound-messages')
|
||||
table = dynamodb.Table(OUTBOUND_TABLE)
|
||||
|
||||
def lambda_handler(event, context):
|
||||
print(f"Received event: {event}")
|
||||
"""
|
||||
Verarbeitet SES Events:
|
||||
- Bounce Events: Speichert bounce details in DynamoDB
|
||||
- Send Events: Ignoriert (nicht mehr benötigt)
|
||||
"""
|
||||
|
||||
detail = event.get('detail', {})
|
||||
mail = detail.get('mail', {})
|
||||
msg_id = mail.get('messageId')
|
||||
print(f"Received event: {json.dumps(event)}")
|
||||
|
||||
if not msg_id:
|
||||
print("No MessageId in event")
|
||||
return
|
||||
# SNS Wrapper entpacken
|
||||
for record in event.get('Records', []):
|
||||
if 'Sns' in record:
|
||||
message = json.loads(record['Sns']['Message'])
|
||||
else:
|
||||
message = record
|
||||
|
||||
event_type = message.get('eventType')
|
||||
|
||||
if event_type == 'Bounce':
|
||||
handle_bounce(message)
|
||||
elif event_type == 'Send':
|
||||
# Ignorieren - wird nicht mehr benötigt
|
||||
print(f"Ignoring Send event (no longer needed)")
|
||||
else:
|
||||
print(f"Unknown event type: {event_type}")
|
||||
|
||||
# Event-Type aus dem Event extrahieren
|
||||
event_type = detail.get('eventType')
|
||||
|
||||
if event_type == 'Send':
|
||||
source = mail.get('source')
|
||||
destinations = mail.get('destination', [])
|
||||
return {'statusCode': 200}
|
||||
|
||||
|
||||
def handle_bounce(message):
|
||||
"""
|
||||
Verarbeitet Bounce Events und speichert Details in DynamoDB
|
||||
"""
|
||||
try:
|
||||
bounce = message.get('bounce', {})
|
||||
mail = message.get('mail', {})
|
||||
|
||||
# Extrahiere relevante Daten
|
||||
feedback_id = bounce.get('feedbackId') # Das ist die Message-ID!
|
||||
bounce_type = bounce.get('bounceType', 'Unknown')
|
||||
bounce_subtype = bounce.get('bounceSubType', 'Unknown')
|
||||
bounced_recipients = [r['emailAddress'] for r in bounce.get('bouncedRecipients', [])]
|
||||
timestamp = bounce.get('timestamp')
|
||||
|
||||
# Original Message Daten
|
||||
original_source = mail.get('source')
|
||||
original_message_id = mail.get('messageId')
|
||||
|
||||
if not feedback_id:
|
||||
print(f"Warning: No feedbackId in bounce event")
|
||||
return
|
||||
|
||||
print(f"Processing bounce: feedbackId={feedback_id}, type={bounce_type}/{bounce_subtype}")
|
||||
print(f"Bounced recipients: {bounced_recipients}")
|
||||
|
||||
# Speichere in DynamoDB (feedback_id ist die Message-ID der Bounce-Mail!)
|
||||
table.put_item(
|
||||
Item={
|
||||
'MessageId': msg_id,
|
||||
'source': source,
|
||||
'destinations': destinations,
|
||||
'timestamp': mail.get('timestamp')
|
||||
'MessageId': feedback_id, # Primary Key
|
||||
'original_message_id': original_message_id, # SES MessageId der Original-Mail
|
||||
'original_source': original_source,
|
||||
'bounceType': bounce_type,
|
||||
'bounceSubType': bounce_subtype,
|
||||
'bouncedRecipients': bounced_recipients, # Liste von Email-Adressen
|
||||
'timestamp': timestamp or datetime.utcnow().isoformat(),
|
||||
'event_type': 'bounce'
|
||||
}
|
||||
)
|
||||
print(f"Stored SEND event for {msg_id}")
|
||||
return
|
||||
|
||||
print(f"✓ Stored bounce info for feedbackId {feedback_id}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error handling bounce: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if event_type == 'Bounce':
|
||||
bounce = detail.get('bounce', {})
|
||||
bounced = [
|
||||
r.get('emailAddress')
|
||||
for r in bounce.get('bouncedRecipients', [])
|
||||
if r.get('emailAddress')
|
||||
]
|
||||
if not bounced:
|
||||
print("No bouncedRecipients in bounce event")
|
||||
return
|
||||
|
||||
table.update_item(
|
||||
Key={'MessageId': msg_id},
|
||||
UpdateExpression="ADD bouncedRecipients :b",
|
||||
ExpressionAttributeValues={
|
||||
':b': set(bounced)
|
||||
}
|
||||
)
|
||||
print(f"Updated {msg_id} with bouncedRecipients={bounced}")
|
||||
return
|
||||
|
||||
if event_type == 'Complaint':
|
||||
complaint = detail.get('complaint', {})
|
||||
complained = [
|
||||
r.get('emailAddress')
|
||||
for r in complaint.get('complainedRecipients', [])
|
||||
if r.get('emailAddress')
|
||||
]
|
||||
if not complained:
|
||||
return
|
||||
|
||||
table.update_item(
|
||||
Key={'MessageId': msg_id},
|
||||
UpdateExpression="ADD complaintRecipients :c",
|
||||
ExpressionAttributeValues={
|
||||
':c': set(complained)
|
||||
}
|
||||
)
|
||||
print(f"Updated {msg_id} with complaintRecipients={complained}")
|
||||
return
|
||||
def handle_send(message):
|
||||
"""
|
||||
DEPRECATED - Wird nicht mehr benötigt
|
||||
Send Events werden jetzt ignoriert
|
||||
"""
|
||||
pass
|
||||
Reference in New Issue
Block a user