Skip to content

Commit 7a601ad

Browse files
DEVOPS-62 sendgrid email with attachments
1 parent 1987c0b commit 7a601ad

File tree

5 files changed

+218
-1
lines changed

5 files changed

+218
-1
lines changed

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ name = "pypi"
77
argparse = "=1.4.0"
88
requests = "=2.31.0"
99
python-dotenv = "=1.0.1"
10+
Jinja2 = "=3.1.3"
11+
sendgrid = "=6.11.0"
1012

1113
[requires]
1214
python_version = "3"

Pipfile.lock

Lines changed: 98 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

email_template.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{{ org_name }} & {{ username }} billing details </title>
5+
</head>
6+
<body>
7+
<p>This report is generated from {{ repo_name }} repository for the below GitHub organization and user account. Check the weekly billing details.
8+
Report generated on : {{ date }}
9+
<li>GitHub organization: {{ org_name }}</li>
10+
<li>GitHub User account : {{ username }}</li>
11+
</p>
12+
13+
</body>
14+
</html>

send_email.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import base64
2+
import os
3+
from sendgrid.helpers.mail import (
4+
Mail, Attachment, FileContent, FileName,
5+
FileType, Disposition, ContentId)
6+
from sendgrid import SendGridAPIClient
7+
from dotenv import load_dotenv
8+
9+
repo_name = "github-automation-to-fetch-remaining-github-runner-time"
10+
org_name = 'devwithkrishna'
11+
username = 'githubofkrishnadhas'
12+
message = Mail(
13+
from_email='[email protected]',
14+
to_emails='[email protected]',
15+
subject='Sending with Twilio SendGrid is Fun',
16+
plain_text_content="Please check your weekly runner details",
17+
html_content='<strong> 123 and easy to do anywhere, even with Python 321</strong>'
18+
)
19+
file_path = 'org_billing_details.json'
20+
with open(file_path, 'rb') as f:
21+
data = f.read()
22+
f.close()
23+
encoded = base64.b64encode(data).decode()
24+
attachment = Attachment()
25+
attachment.file_content = FileContent(encoded)
26+
attachment.file_type = FileType('application/json')
27+
attachment.disposition = Disposition('attachment')
28+
message.attachment = attachment
29+
try:
30+
load_dotenv()
31+
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID-API-KEY'))
32+
response = sendgrid_client.send(message)
33+
print(response.status_code)
34+
print(response.body)
35+
print(response.headers)
36+
except Exception as e:
37+
print(str(e))

send_email_using_sendgrid.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import base64
2+
import os
3+
from datetime import datetime
4+
from sendgrid.helpers.mail import (
5+
Mail, Attachment, FileContent, FileName, Content,
6+
FileType, Disposition, ContentId)
7+
from sendgrid import SendGridAPIClient
8+
from dotenv import load_dotenv
9+
from jinja2 import Environment,FileSystemLoader
10+
11+
def formatted_datetime():
12+
# Get current date and time
13+
now = datetime.now()
14+
15+
# Format date and time as dd:mm:yyyy hh:minur
16+
formatted_date_time = now.strftime("%d-%m-%Y %H:%M")
17+
return formatted_date_time
18+
def send_email_with_sendgrid():
19+
"""
20+
send email using sendgrid.
21+
:return:
22+
"""
23+
# Variables for template
24+
repo_name = "github-automation-to-fetch-remaining-github-runner-time"
25+
org_name = 'devwithkrishna'
26+
username = 'githubofkrishnadhas'
27+
date = formatted_datetime()
28+
# Load the template file
29+
file_loader = FileSystemLoader('.')
30+
env = Environment(loader=file_loader)
31+
template = env.get_template('email_template.html')
32+
33+
# Render the template with dynamic data
34+
html_content = template.render(repo_name=repo_name, org_name=org_name, username=username, date=date)
35+
message = Mail(
36+
from_email='[email protected]',
37+
to_emails='[email protected]',
38+
subject=f'{ org_name } & { username } billing details ',
39+
html_content= Content("text/html", html_content)
40+
)
41+
file_path = 'org_billing_details.json'
42+
with open(file_path, 'rb') as f:
43+
data = f.read()
44+
f.close()
45+
encoded = base64.b64encode(data).decode()
46+
attachment = Attachment()
47+
attachment.file_content = FileContent(encoded)
48+
attachment.file_type = FileType('application/json')
49+
attachment.file_name = FileName('org_billing_details.json')
50+
attachment.disposition = Disposition('attachment')
51+
message.attachment = attachment
52+
try:
53+
load_dotenv()
54+
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID-API-KEY'))
55+
response = sendgrid_client.send(message)
56+
print(response.status_code)
57+
print(response.body)
58+
print(response.headers)
59+
except Exception as e:
60+
print(e)
61+
62+
def main():
63+
""" To test the script"""
64+
send_email_with_sendgrid()
65+
66+
if __name__ == '__main__':
67+
main()

0 commit comments

Comments
 (0)