-
Notifications
You must be signed in to change notification settings - Fork 43
/
parse_and_email.py
59 lines (51 loc) · 1.88 KB
/
parse_and_email.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
from __future__ import print_function
import urllib
import boto3
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
print('Loading function')
s3 = boto3.resource('s3')
local_folder = "/tmp/"
def sendMail(FROM,TO,SUBJECT,TEXT):
import smtplib
# your AWS SES smtp
SERVER = ''
EMAIL_USERNAME = ''
EMAIL_PASSWORD = ''
"""this is some test documentation in the function"""
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = SUBJECT
part1 = MIMEText(TEXT, 'plain', 'utf-8')
msg.attach(part1)
# if you want to send HTML email, uncomment the following two lines
# part2 = MIMEText(TEXT, 'html', 'utf-8')
#msg.attach(part2)
# Send the mail
server = smtplib.SMTP(SERVER)
"New part"
server.starttls()
server.login(EMAIL_USERNAME, EMAIL_PASSWORD)
server.sendmail(FROM, TO, msg.as_string())
server.quit()
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
try:
print("Saving file {} to folder {}".format(key,local_folder))
s3.Bucket(bucket).download_file(key, local_folder+key)
with open(local_folder+key, 'r') as content_file:
content = content_file.read()
print(content)
try:
sendMail('email@example.com','email@example.com', 'File '+ key , content)
print("Email sent with subject {}".format(key))
except Exception as e:
print(e)
raise(e)
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e