-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemailer.py
More file actions
57 lines (47 loc) · 2.03 KB
/
Copy pathemailer.py
File metadata and controls
57 lines (47 loc) · 2.03 KB
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
from email import message
import smtplib, ssl, re, os, subprocess, sys, shutil
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
load_dotenv()
class Emailer:
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
servicesEnabled = False
sender_email = None
password = None
contextChecked = False
@staticmethod
def checkContext():
if "EmailingServicesEnabled" in os.environ and os.environ['EmailingServicesEnabled'] == 'True':
Emailer.sender_email = os.environ["AppEmail"]
Emailer.password = os.environ['EmailAppPassword']
Emailer.servicesEnabled = True
Emailer.contextChecked = True
@staticmethod
def sendEmail(destEmail, subject, altText, html):
## Email bypass
if not Emailer.contextChecked:
print("EMAILER Error: System context was not checked before sending email. Skipping email.")
return True
if not Emailer.servicesEnabled:
print("EMAILER: Emailing services are not enabled. Skipping email.")
return True
try:
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = Emailer.sender_email
message["To"] = destEmail
part1 = MIMEText(altText, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
context = ssl._create_unverified_context()
with smtplib.SMTP_SSL(Emailer.smtp_server, Emailer.port, context=context) as server:
server.login(Emailer.sender_email, Emailer.password)
server.sendmail(Emailer.sender_email, destEmail, message.as_string())
print("EMAILER: Email sent to {}".format(destEmail))
return True
except Exception as e:
print("EMAILER ERROR: Failed to send email to {} with subject {}. Error: {}".format(destEmail, subject, e))
return False