Untitled
unknown
plain_text
2 years ago
3.6 kB
13
Indexable
import os
import boto3
from datetime import datetime, timedelta
def lambda_handler(event, context):
query_from_dynamoDB()
def query_from_dynamoDB() :
alert_date = datetime.now().date() + timedelta(days = 30)
alert_date = datetime.strftime(alert_date, format = "%Y-%m-%d")
dynamodb_client = boto3.client("dynamodb")
response = dynamodb_client.query(
TableName = 'test-table',
IndexName = 'expiryDate',
Select = 'ALL_ATTRIBUTES',
KeyConditions = {
'dummyCol' : {
'AttributeValueList' : [{
'S' : 'A'
}],
'ComparisonOperator' : 'EQ'
},
'expiryDate' : {
'AttributeValueList' : [{ # expiry_date - today < 30
'S' : alert_date # expiry_date < today + 30
}],
'ComparisonOperator' : 'LT'
}
}
)
Send_email_SES(response['Items'])
def Generate_email_data(recipient, items) :
recipient_name = recipient.split("@")[0]
body = f"Dear <b>{recipient_name}</b>, <br> <br> You got <b>{len(items)} domain(s)</b> about to expire, here is the detail : \n"
table_content = "<!DOCTYPE html>\n<html>\n<head>\n<title>Page Title</title>\n</head>\n<body>\n"
attributes = ['Domain name', 'Expiry date', 'Registration date']
table_content += "<br><br><table border=\"1\" cellspacing=\"0\" cellpadding=\"10\" width=\"50%\">\n"
# Header part
table_content += " <thead>\n <tr align = \"center\">\n"
for col in attributes : table_content += f" <th>{col}</th>\n"
table_content += " </tr>\n </thead>\n"
# Body part
table_content += " <tbody>\n"
for item in items :
date_diff = datetime.strptime(item['expiryDate']['S'], "%Y-%m-%d").date() - datetime.now().date()
table_content += " <tr align = \"center\">\n"
table_content += f" <td width=\"25%\">{item['domainName']['S']}</td>\n"
table_content += f" <td width=\"50%\">{item['expiryDate']['S']} <b><i>({date_diff.days} days until expire)</b></i></td>\n"
table_content += f" <td width=\"25%\">{item['registrationDate']['S']}</td>\n"
table_content += " </tr>\n"
table_content += " </tbody>\n"
table_content += "</table>\n<br><br>"
table_content += "</body>\n</html>"
end_body = "Please ask for extension the expiry date of the domain(s) above.<br><br>Best regards,<br><br>\t STYL Solutions"
full_body = body + table_content + end_body
return full_body
def Send_email_SES(items) :
ses_client = boto3.client("ses")
recipient_list = ['dangquangvkl@gmail.com', 'tient4199@gmail.com', 'pham.tan.anh.vu@styl.solutions']
Subject = {
"Data" : f"ALERT ! You got {len(items)} domain(s) about to expire !",
'Charset' : 'UTF-8'
}
for recipient in recipient_list :
response = ses_client.send_email(
Source = "anhvuphamtan@gmail.com",
Destination = {
"ToAddresses" : [recipient]
},
Message = {
'Subject' : Subject,
'Body' : {
'Html' : {
'Data' : Generate_email_data(recipient, items)
}
}
}
)
query_from_dynamoDB()Editor is loading...