email-amazon/lambda_function_outbound.py

73 lines
2.0 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):
detail = event['detail']
mail = detail['mail']
msg_id = mail['messageId']
source = mail['source']
destinations = mail['destination'] # Liste
if not msg_id:
print("No MessageId in event")
return
if event_type == 'SEND':
# wie bisher
source = mail.get('source')
destinations = mail.get('destination', [])
table.put_item(
Item={
'MessageId': msg_id,
'source': source,
'destinations': destinations,
'timestamp': mail.get('timestamp')
}
)
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
# in DynamoDB anhängen
table.update_item(
Key={'MessageId': msg_id},
UpdateExpression="ADD bouncedRecipients :b",
ExpressionAttributeValues={
':b': set(bounced) # String-Set
}
)
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