-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
73 lines (67 loc) · 3 KB
/
send_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
from date_time import date_time
from sendgrid.helpers.mail import Mail,Content
from sendgrid import SendGridAPIClient
from dotenv import load_dotenv
from jinja2 import Environment, FileSystemLoader, select_autoescape
# def formatted_datetime():
# # Get current date and time
# now = datetime.now()
#
# # Format date and time as dd:mm:yyyy hh:minur
# formatted_date_time = now.strftime("%d-%m-%Y %H:%M")
# return formatted_date_time
def send_email_with_sendgrid(applications_list_final_with_expiry_dates: list[dict]):
"""
send email using sendgrid.
json_files --> list of all json files generated
:return:
"""
success_list = []
for application in applications_list_final_with_expiry_dates:
# Variables for template
email_ids = application["owner_email_ids"]
print(f'Owners of {application["app_display_name"]} are {email_ids}')
manager = "Krishnadhas N K"
manager_mail = "[email protected]"
app_name = application["app_display_name"]
expiry_status = application["expiry_status"]
secret_id = application["secret_id"]
created_date = application["created_date_time"]
expiry_date = application["app_expiry_datetime"]
app_id = application["app_id"]
date = date_time()
# send the alert if the expiry date is close to 30 days else ignore till next run
if (expiry_status == "No immediate action needed") or (expiry_status == 'No secret created for the App'):
continue
# Load the template file
file_loader = FileSystemLoader('.')
env = Environment(loader=file_loader, autoescape=select_autoescape(['html', 'xml']))
template = env.get_template('email_template.html')
# Render the template with dynamic data
html_content = template.render(app_name=app_name, expiry_status=expiry_status, secret_id=secret_id, date=date,
manager_mail=manager_mail,app_id=app_id,manager=manager, created_date=created_date, expiry_date=expiry_date)
message = Mail(
from_email='[email protected]',
to_emails=email_ids,
subject=f'Azure App credential expiry alert for {app_name} - {expiry_status}',
html_content= Content("text/html", html_content)
)
success_list.append({
'app_name': app_name,
'expiry_status': expiry_status,
'secret_id': secret_id,
'app_id': app_id,
'created_date': created_date,
'expiry_date': expiry_date,
'app_owners': email_ids,
})
try:
load_dotenv()
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message)
print(f'Status code: {response.status_code}')
print(f'Communication was send to {email_ids} for {app_name} at {date}')
except Exception as e:
print(e)
return success_list