-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailparser.py
66 lines (50 loc) · 1.92 KB
/
mailparser.py
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
import boto3
from config import get_rules
from mail import Mail, ParsedMail
from sheets import update_sheet
MAIL_BUCKET_NAME = "mailparser2"
def lambda_handler(event, context):
"""Reads email details from an SES trigger event.
Args:
event: The SES trigger event containing email details.
context: The Lambda context object.
Returns:
A dictionary containing details about the processed email.
"""
# Get the email details from the event
records = event['Records']
for record in records:
ses_message = record['ses']['mail']
# TODO: test
s3_client = boto3.client('s3')
object_key = ses_message['messageId']
email_content = s3_client.get_object(Bucket=MAIL_BUCKET_NAME, Key=object_key)['Body'].read()
print(email_content)
print(ses_message)
mail = Mail(ses_message['messageId'], email_content.decode('utf-8'))
parsedmail = ParsedMail(mail)
parsedmail.parse()
rules = get_rules(parsedmail)
data = parsedmail.generate_sheet_data(rules["row"])
update_sheet(rules["sheetId"], data, rules["sheetName"])
# Extract relevant email details
sender = ses_message['source']
recipient = ses_message['destination'][0]
subject = None
body = None
# Extract headers (optional)
for header in ses_message.get('headers', []):
if header['name'] == 'Subject':
subject = header['value']
elif header['name'] == 'Content-Type' and header['value'].startswith('text/plain'):
body = ses_message.get('body', '')
break # Only process plain text bodies for simplicity
# Process the email details (replace with your logic)
print(f"Received email from {sender} to {recipient}")
print(f"Subject: {subject}")
print(f"Body: {body}")
# You can further process the email details here
# For example, store them in a database, send notifications, etc.
return {
'message': 'Successfully processed email details'
}