This repository was archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathses_lambda_forwarder.py
More file actions
83 lines (76 loc) · 2.97 KB
/
ses_lambda_forwarder.py
File metadata and controls
83 lines (76 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# coding: utf-8
import boto3
import email
import json
import logging
import os
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def handler(event, context):
logger.info("Collecting event record data...")
record = event["Records"][0]
try:
logger.info("Looking for SES event...")
bucket_name = os.environ['SESACMS3BucketName']
message_id = record["ses"]["mail"]["messageId"]
message_source = record["ses"]["mail"]["source"]
message_destination = record["ses"]["mail"]["destination"]
except KeyError:
logger.critical("There was a problem retrieving data "
"from the event record, {}".format(record))
return("FAIL")
s3_client = boto3.client('s3')
logger.info("Fetching s3 object: {}/{}".format(bucket_name, message_id))
mail_object = s3_client.get_object(Bucket = bucket_name, Key = message_id)
logger.info("Decoding mail body...")
email_data = mail_object["Body"].read().decode('utf-8')
# Get env variables
# We need to use a verified email address rather than relying on the source
logger.info("Retrieving environment settings...")
email_from = os.environ['FromAddress']
filter_addresses = [address.strip() for address in os.environ['FilterAddresses'].split(",")]
forwarding_addresses = [address.strip() for address in os.environ['ForwardingAddresses'].split(",")]
# Filter out addresses
filtered_addresses = [address for address in message_destination if address in filter_addresses]
if len(filtered_addresses) == 0:
logger.debug("No filtering addresses found, skipping message...")
return ("CONTINUE")
logger.info("Found filtering addresses {}, "
"forwarding the message...".format(','.join(filtered_addresses)))
email_object = email.message_from_string(email_data)
email_subject = email_object.get('Subject', 'Verification message for ACM')
logger.info("Parsing mail: {}".format(email_subject))
email_text=""
for part in email_object.walk():
c_type = part.get_content_type()
c_disp = part.get('Content-Disposition')
if c_type == 'text/plain' and c_disp == None:
email_text = email_text + '\n' + part.get_payload()
else:
continue
logger.info("Connecting to SES client")
ses_client = boto3.client('ses')
response = ses_client.send_email(
Source=email_from,
Destination={
'ToAddresses': forwarding_addresses
},
Message={
'Subject': {
'Data': email_subject,
},
'Body': {
'Text': {
'Data': email_text,
}
}
},
Tags=[
{
'Name': 'string',
'Value': 'string'
},
],
)
logger.info("Sent verification email successfully to {}".format(','.join(forwarding_addresses)))
return "CONTINUE"