-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_manager.py
More file actions
54 lines (48 loc) · 1.97 KB
/
notification_manager.py
File metadata and controls
54 lines (48 loc) · 1.97 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
from twilio.rest import Client
import smtplib
import dotenv
import os
dotenv.load_dotenv()
TWILIO_SID = os.getenv("TWILIO_SID")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_VIRTUAL_NUMBER = os.getenv("TWILIO_VIRTUAL_NUMBER")
TWILIO_VERIFIED_NUMBER = os.getenv("TWILIO_VERIFIED_NUMBER")
MAIL_PROVIDER_SMTP_ADDRESS = os.getenv("MAIL_PROVIDER_SMTP_ADDRESS")
MY_EMAIL = os.getenv("MY_EMAIL")
MY_PASSWORD = os.getenv("MY_PASSWORD")
class NotificationManager:
def __init__(self):
self.client = Client(TWILIO_SID, TWILIO_AUTH_TOKEN)
def send_sms(self, message):
message = self.client.messages.create(
body=message,
from_=TWILIO_VIRTUAL_NUMBER,
to=TWILIO_VERIFIED_NUMBER,
)
def send_emails(self, emails, message):
try:
if "gmail" in MAIL_PROVIDER_SMTP_ADDRESS:
with smtplib.SMTP_SSL(MAIL_PROVIDER_SMTP_ADDRESS, 465) as connection:
connection.login(MY_EMAIL, MY_PASSWORD)
for email in emails:
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=email,
msg=f"Subject:New Low Price Flight!\n\n{message}".encode(
"utf-8"
),
)
else:
with smtplib.SMTP(MAIL_PROVIDER_SMTP_ADDRESS) as connection:
connection.starttls()
connection.login(MY_EMAIL, MY_PASSWORD)
for email in emails:
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=email,
msg=f"Subject:New Low Price Flight!\n\n{message}".encode(
"utf-8"
),
)
except Exception as e:
print("Connection error! Error: " + str(e))