-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathlambda_s3_event_ses_attach_email.py
More file actions
80 lines (64 loc) · 2.23 KB
/
lambda_s3_event_ses_attach_email.py
File metadata and controls
80 lines (64 loc) · 2.23 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
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import os
import ast
import json
import boto3
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
ses = boto3.client("ses")
s3 = boto3.client("s3")
"""
Set SENDER_EMAIL & TO_EMAILS as the lambda environment variables
For example:
SENDER_EMAIL : email
TO_EMAILS : ['email1', 'email2']
"""
SENDER_EMAIL = os.environ["SENDER_EMAIL"]
TO_EMAILS = ast.literal_eval(os.environ["TO_EMAILS"])
def send_email(sender, receiver, subject, body, file_content, file_name):
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = ",".join(receiver)
body_txt = MIMEText(body, "html")
attachment = MIMEApplication(file_content)
attachment.add_header("Content-Disposition", "attachment", filename=file_name)
msg.attach(body_txt)
msg.attach(attachment)
response = ses.send_raw_email(
Source=sender, Destinations=receiver, RawMessage={"Data": msg.as_string()}
)
return response
def lambda_handler(event, context):
if "Records" in event:
records = event["Records"][0]
action = records["eventName"]
ip = records["requestParameters"]["sourceIPAddress"]
bucket_name = records["s3"]["bucket"]["name"]
file_object = records["s3"]["object"]["key"]
fileObj = s3.get_object(Bucket=bucket_name, Key=file_object)
file_content = fileObj["Body"].read()
file_name = file_object.split("/")[-1]
subject = f"{str(action)} Event from {bucket_name}"
body = """
<br>
This email is to notify you regarding {} event.
The object {} is Uploaded.
Source IP: {}
""".format(
action, object, ip
)
response = send_email(
SENDER_EMAIL, TO_EMAILS, subject, body, file_content, file_name
)
if response["ResponseMetadata"]["HTTPStatusCode"] == 200:
return {"statusCode": 200, "body": json.dumps("Email sent successfully!")}
return {"statusCode": 500, "body": json.dumps("Email delivery failed!")}