Spaces:
Running
Running
Commit
·
2b2b0d0
1
Parent(s):
43737a5
Refactor send_email function to use Sendinblue API for sending emails
Browse files- send_mail.py +38 -62
send_mail.py
CHANGED
@@ -1,20 +1,16 @@
|
|
1 |
-
import os
|
2 |
-
import smtplib
|
3 |
-
from email.mime.text import MIMEText
|
4 |
-
from email.mime.multipart import MIMEMultipart
|
5 |
-
from email.mime.base import MIMEBase
|
6 |
from email import encoders
|
|
|
|
|
|
|
|
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
load_dotenv()
|
10 |
-
|
11 |
-
smtp_server = os.getenv("SERVER")
|
12 |
-
smtp_login = os.getenv("LOGIN")
|
13 |
-
smtp_passwd = os.getenv("PASSWD")
|
14 |
|
15 |
-
def mail_body(
|
16 |
body = f"""
|
17 |
-
Hello
|
18 |
Thankyou for using Project Gatekeeper to generate your SSL certificate.
|
19 |
Your SSL certificate has been generated and is attached to this email.
|
20 |
Please find the attached file for your SSL certificate.
|
@@ -28,56 +24,36 @@ def mail_body(email, generation_details):
|
|
28 |
"""
|
29 |
return body
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
def perpare_pvt(pvt):
|
38 |
-
filename = "private_key.key"
|
39 |
-
pvt= pvt.encode('utf-8')
|
40 |
-
pvtkey = MIMEBase('application', 'octet-stream')
|
41 |
-
pvtkey.set_payload(pvt)
|
42 |
-
encoders.encode_base64(pvtkey)
|
43 |
-
pvtkey.add_header('Content-Disposition', "attachment; filename= " + filename)
|
44 |
-
return pvtkey
|
45 |
-
|
46 |
-
def perpare_csr(csr):
|
47 |
-
filename = "domain.csr"
|
48 |
-
csr= csr.encode('utf-8')
|
49 |
-
domaincsr = MIMEBase('application', 'octet-stream')
|
50 |
-
domaincsr.set_payload(csr)
|
51 |
-
encoders.encode_base64(domaincsr)
|
52 |
-
domaincsr.add_header('Content-Disposition', "attachment; filename= " + filename)
|
53 |
-
return domaincsr
|
54 |
-
|
55 |
-
def perpare_ssl(ssl):
|
56 |
-
filename = "ssl_certificate.crt"
|
57 |
-
ssl= ssl.encode('utf-8')
|
58 |
-
sslcrt = MIMEBase('application', 'octet-stream')
|
59 |
-
sslcrt.set_payload(ssl)
|
60 |
-
encoders.encode_base64(sslcrt)
|
61 |
-
sslcrt.add_header('Content-Disposition', "attachment; filename= " + filename)
|
62 |
-
return sslcrt
|
63 |
-
|
64 |
-
def prepare_email(email, private_key, csr, cert, generation_details):
|
65 |
-
body = mail_body(email, generation_details)
|
66 |
-
msg = MIMEMultipart()
|
67 |
-
msg['From'] = "Project Gatekeeper <{}>".format(smtp_login)
|
68 |
-
msg['To'] = email
|
69 |
-
msg['Subject'] = "Project Gatekeeper - Your SSL Certificate is ready!"
|
70 |
-
msg.attach(MIMEText(body, 'plain'))
|
71 |
-
p, c, s = make_attachment(private_key, csr, cert)
|
72 |
-
for attachment_package in [p, c, s]:
|
73 |
-
msg.attach(attachment_package)
|
74 |
-
text = msg.as_string()
|
75 |
-
return text
|
76 |
|
77 |
def send_email(email, private_key, csr, cert, generation_details):
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from email import encoders
|
2 |
+
from email.mime.base import MIMEBase
|
3 |
+
import os
|
4 |
+
import sib_api_v3_sdk
|
5 |
+
from sib_api_v3_sdk.rest import ApiException
|
6 |
from dotenv import load_dotenv
|
7 |
|
8 |
load_dotenv()
|
9 |
+
mail_api = os.getenv("MAIL_API")
|
|
|
|
|
|
|
10 |
|
11 |
+
def mail_body(generation_details):
|
12 |
body = f"""
|
13 |
+
Hello,
|
14 |
Thankyou for using Project Gatekeeper to generate your SSL certificate.
|
15 |
Your SSL certificate has been generated and is attached to this email.
|
16 |
Please find the attached file for your SSL certificate.
|
|
|
24 |
"""
|
25 |
return body
|
26 |
|
27 |
+
def create_attachment(content, filename):
|
28 |
+
attachment = MIMEBase('application', 'octet-stream')
|
29 |
+
attachment.set_payload(content)
|
30 |
+
encoders.encode_base64(attachment)
|
31 |
+
attachment.add_header('Content-Disposition', f'attachment; filename="{filename}"')
|
32 |
+
return attachment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def send_email(email, private_key, csr, cert, generation_details):
|
35 |
+
configuration = sib_api_v3_sdk.Configuration()
|
36 |
+
configuration.api_key['api-key'] = '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'
|
37 |
+
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
|
38 |
+
|
39 |
+
data = mail_body(generation_details)
|
40 |
+
p_attachment = create_attachment(private_key.encode('utf-8'), "private_key.txt")
|
41 |
+
c_attachment = create_attachment(csr.encode('utf-8'), "csr.txt")
|
42 |
+
s_attachment = create_attachment(cert.encode('utf-8'), "cert.txt")
|
43 |
+
|
44 |
+
subject = "Project Gatekeeper - Your SSL Certificate is ready!"
|
45 |
+
sender = {"name": "Project Gatekeeper", "email": "[email protected]"}
|
46 |
+
reply_to = {"name": "Project Gatekeeper", "email": "[email protected]"}
|
47 |
+
text_content = data
|
48 |
+
attachments = [
|
49 |
+
{"content": p_attachment.get_payload(), "name": p_attachment.get_filename()},
|
50 |
+
{"content": c_attachment.get_payload(), "name": c_attachment.get_filename()},
|
51 |
+
{"content": s_attachment.get_payload(), "name": s_attachment.get_filename()},
|
52 |
+
]
|
53 |
+
to = [{"email": email}]
|
54 |
+
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, reply_to=reply_to, attachment=attachments, text_content=text_content, sender=sender, subject=subject)
|
55 |
+
try:
|
56 |
+
api_response = api_instance.send_transac_email(send_smtp_email)
|
57 |
+
print(api_response)
|
58 |
+
except ApiException as e:
|
59 |
+
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
|