-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_email_with_attachment.py
More file actions
42 lines (33 loc) · 1.27 KB
/
s3_email_with_attachment.py
File metadata and controls
42 lines (33 loc) · 1.27 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
import boto3
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
def lambda_handler(event, context):
ses = boto3.client("ses")
s3 = boto.client("s3")
for i in event['Records']:
action = i['eventName']
ip = i['requestParameters']['sourceIPAddress']
bucket_name = i['s3']['bucket']['name']
object = i['s3']['object']['key']
fileObj = s3.get_object(Bucket=bucket_name, Key=object)
file_content = fileObj["Body"].read()
sender = "javed7504@gmail.com"
to = "javed7504@gmail.com"
subject = str(action) + 'Event from' + bucket_name
body = """
<br>
This email is to notify you regarding {} event.
The object {} is deleted.
Source IP: {}
""".format(action, object, ip)
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = to
body_txt = MIMEText(body, "html")
attachment = MIMEApplication(file_content)
attachment.add_header("Content-Disposition", "attachment", filename="demo.txt")
msg.attach(body_txt)
msg.attach(attachment)
response = ses.send_raw_email(Source=sender, Destination=[to], RawMessage={"Data": msg.as_string()})