-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
81 lines (66 loc) · 1.83 KB
/
handler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import boto3
from botocore.exceptions import ClientError
import json
import os
import uuid
import decimal
client = boto3.client('ses')
sender = os.environ['SENDER_EMAIL']
subject = os.environ['EMAIL_SUBJECT']
configset = os.environ['CONFIG_SET']
charset = 'UTF-8'
dynamodb = boto3.resource('dynamodb')
def list(event, context):
table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
result = table.scan()
return result['Items']
def sendMail(event, context):
print(event)
try:
data = event['body']
content = 'Opinion from ' + data['name'] + data['email'] + ',\nContents: ' + data['message']
saveToDynamoDB(data)
response = sendMailToUser(data, content)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message Id:"),
print(response['MessageId'])
return "Email sent!"
def saveToDynamoDB(data):
table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
item = {
'id': str(uuid.uuid1()),
'name': data['name'],
'firm': data['firm'],
'email': data['email'],
'message': data['message'],
'published': False,
}
table.put_item(Item=item)
return
def sendMailToUser(data, content):
return client.send_email(
Source=sender,
Destination={
'ToAddresses': [
'<<put.your.email.here.before.deploy>>',
],
},
Message={
'Subject': {
'Charset': charset,
'Data': subject
},
'Body': {
'Html': {
'Charset': charset,
'Data': content
},
'Text': {
'Charset': charset,
'Data': content
}
}
}
)