-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSendEmail.py
101 lines (87 loc) · 3.13 KB
/
SendEmail.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#Create new Python Lambda with below Code.
#Enter following values.
#SENDER, ADMINEMAIL, region_name, user, password, account, warehouse, database, and schema
import json
import snowflake.connector
import logging
import datetime as dt
from datetime import datetime
from botocore.exceptions import ClientError
import boto3
import base64
logging.getLogger('snowflake.connector').setLevel(logging.WARNING)
for name in logging.Logger.manager.loggerDict.keys():
if 'snowflake' in name:
logging.getLogger(name).setLevel(logging.WARNING)
logging.getLogger(name).propagate = False
CHARSET = "UTF-8"
SENDER = '@email.com'#Sender Email
ADMINEMAIL = 'email.com' #Enter admin email address ex: ['[email protected]'] or a list with (,) separated
html_body = """<html>
<head></head>
<body>
<h1>{header}</h1>
<p>
<br>{errordetails}<br>
</p>
</body>
</html>
"""
def sendmail(errormessage,subject,RECIPIENTS):
try:
client = boto3.client('ses',region_name="Enter Region")
htmlHeader = "Snowflake Notification"
response = client.send_email(
Destination={
'ToAddresses': RECIPIENTS,
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': html_body.format(header=htmlHeader,errordetails=errormessage),
},
},
'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'])
def lambda_handler(event, context):
# TODO implement
ctx = snowflake.connector.connect(user='Enter Username', password='Enter Password',role="Role_Name",account='Account_name',warehouse="Warehouse_name",database="DB_Name",schema="Schema_Name")
cs = ctx.cursor()
try:
cs.execute("SELECT ID,TO_ADDRESS,SUBJECT,MESSAGE FROM NOTIFICATIONLIST WHERE ISPROCESSED = FALSE AND ERR_MESSAGE IS NULL ORDER BY ID asc LIMIT 100")
for (ID,TO_ADDRESS,SUBJECT,MESSAGE) in cs:
MM = {}
MM['to'] = TO_ADDRESS
MM['subject'] = SUBJECT
MM['message'] = MESSAGE
print(MM)
if ',' in TO_ADDRESS:
to_list = (TO_ADDRESS).split(",")
else:
to_list = [TO_ADDRESS]
sendmail(MESSAGE,SUBJECT,to_list)
cs.execute("UPDATE NOTIFICATIONLIST set ISPROCESSED = TRUE WHERE ID = %s",(ID))
cs.close()
ctx.close()
except Exception as e:
sendmail(e+'\n'+MM,'Snowflake email notifcation error',ADMINEMAIL)
cs.close()
ctx.close()
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
#Create a Role with SES access and attach it to Lambda.
#Under layer select the newly created layer.
#Change the max timeout value to 15 mins and memory to 512 MB.