Last active
August 3, 2020 01:54
-
-
Save noyb34/36c52056509fd6c70fb28941fa6a7ba6 to your computer and use it in GitHub Desktop.
Enables uploaded S3 object[s) to be sent via email attachment over SES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = boto3.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 = "[email protected]" | |
to = "[email protected]" | |
subject = 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) | |
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=object) | |
msg.attach(body_txt) | |
msg.attach(attachment) | |
response = ses.send_raw_email(Source = sender, Destinations = [to], RawMessage = {"Data": msg.as_string()}) | |
return "Thanks" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment