-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambdafunction.py
More file actions
129 lines (99 loc) · 3.37 KB
/
Copy pathlambdafunction.py
File metadata and controls
129 lines (99 loc) · 3.37 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import print_function
from botocore.exceptions import ClientError
import json
import boto3
print('Loading function')
def handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
message = event['Records'][0]['Sns']['Message']
print("From SNS: " + message)
name, email, token = get_email_details(message)
print (name, email, token)
if checktable(email):
print("Email already sent")
print("Email already sent previously")
print("Exiting")
else:
send_email(name, email, token)
update_email(email)
def get_email_details(csvmessage):
valuelist = csvmessage.split(",")
name = valuelist[0]
emailadd = valuelist[1]
token = valuelist[2]
return name, emailadd, token
def checktable(emailadd):
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
email_table = dynamodb.Table('sent-emails')
# table = dynamodb.Table('')
response = email_table.get_item(
Key={'emailadd': emailadd})
if 'Item' in response:
return True
else:
return False
def update_email(emailadd):
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
email_table = dynamodb.Table('sent-emails')
response = email_table.put_item(
Item={'emailadd': emailadd})
# try:
# response = table.get_item(Key={'emailadd': str(emailadd)})
# except boto.dynamodb.exceptions.DynamoDBKeyNotFoundError:
# response = None
# return response
def send_email(recipientname, recipientemailid, authtoken):
SENDER = "no-reply@prod.applicationbhan.me"
RECIPIENT = recipientemailid
authlink = "http://prod.applicationbhan.me/verify?email=" + recipientemailid + "&token=" + authtoken
print(authlink)
DESTINATION = {
'ToAddresses': [
RECIPIENT,
]
}
AWS_REGION = "us-east-1"
SUBJECT = "Verification Email"
BODY_TEXT = ("Email verification for new user\r\n"
"Details:\r\n"
"\n"
"Name: " + recipientname + "\n"
"\n"
"Verifying email id: " + recipientemailid + "\r\n"
"\r\n"
"Use the link provided below to verify yourself:\r\n"
"Verify: " + authlink
)
CHARSET = "UTF-8"
client = boto3.client('ses', region_name=AWS_REGION)
try:
response = client.send_email(
Destination=DESTINATION,
Message={
'Body': {
# 'Html': {
# 'Charset': CHARSET,
# 'Data': BODY_,
# },
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:")
print(response['MessageId'])
print(RECIPIENT)
return {
'statusCode': 200,
'body': json.dumps(response)
}