74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
import boto3
|
|
import os
|
|
|
|
dynamo = boto3.resource('dynamodb', region_name='us-east-2')
|
|
table = dynamo.Table('ses-outbound-messages')
|
|
|
|
def lambda_handler(event, context):
|
|
print(f"Received event: {event}")
|
|
|
|
detail = event.get('detail', {})
|
|
mail = detail.get('mail', {})
|
|
msg_id = mail.get('messageId')
|
|
|
|
if not msg_id:
|
|
print("No MessageId in event")
|
|
return
|
|
|
|
# Event-Type aus dem Event extrahieren
|
|
event_type = detail.get('eventType')
|
|
|
|
if event_type == 'Send':
|
|
source = mail.get('source')
|
|
destinations = mail.get('destination', [])
|
|
table.put_item(
|
|
Item={
|
|
'MessageId': msg_id,
|
|
'source': source,
|
|
'destinations': destinations,
|
|
'timestamp': mail.get('timestamp')
|
|
}
|
|
)
|
|
print(f"Stored SEND event for {msg_id}")
|
|
return
|
|
|
|
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 |