-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_client.py
67 lines (61 loc) · 2.89 KB
/
email_client.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
from logging import exception
import os
import smtplib, ssl
class EmailClient:
def __init__(self) -> None:
# Read Required Environment variables.
try:
# The github action workflow reads git secrets at run time and exports them as env variables to make them available to program.
self.__sender_email_id = os.getenv('SENDER_EMAIL_ID')
self.__sender_email_passwd = os.getenv('SENDER_EMAIL_PASSWORD')
# > Note : Less secure apps are no more supported by gmail. Use an app password instead of actual mail password.
self.__subscribers = str(os.getenv('NOTIFICATION_SUBSCRIBERS')).split(',')
self.__port = 587 # For SSL
except Exception as err:
print()
exit(err)
# Initiate hand shake.
try:
# Create a secure SSL context
context = ssl.create_default_context()
self.__server = smtplib.SMTP("smtp.gmail.com",self.__port)
self.__server.ehlo()
self.__server.starttls(context=context) # Secure the connection
self.__server.ehlo()
except Exception as err:
print(err)
exit("Unexpected Error During HandShake with gmail server!!!")
# Login to server
try:
self.__server.login(self.__sender_email_id, self.__sender_email_passwd)
except smtplib.SMTPAuthenticationError as err:
print(err)
exit("Invalid Username / Password, Authentication Rejected by server.")
except Exception as err:
print(err)
exit("Unable to login to smtp server for unknown reasons!!")
print("Successfully Authenticated with E-Mail Server.")
def SendMail_Wrapper(self, Subject: str, Message: str, Receivers = None):
"""
Wrapper function for building and sending email messages.\n
If None is given for receivers, it will default to subscribers read from env.\n
Otherwise a list of email ids can be supplied to specify receivers.
"""
if Receivers is None:
Receivers = self.__subscribers
Message_Body = 'Subject: {}\n\n{}'.format(Subject, Message) # form message with subject and body.
try:
self.__server.sendmail(self.__sender_email_id, Receivers, Message_Body)
except Exception as err:
print(format(err))
print("Unexpected error while sending email!!")
def release_connection_wrapper(self):
"""
This method has to be called at the end of script.\n
This releases the current server's SMTP handshake, active connection after which no emails can be sent.
"""
try:
self.__server.quit()
except exception as err:
print(err)
exit("Error While Closing SMTP Connection!!")