-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_notifier.py
More file actions
37 lines (29 loc) · 1.1 KB
/
email_notifier.py
File metadata and controls
37 lines (29 loc) · 1.1 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
# email_notifier.py
import logging
import smtplib
from email.message import EmailMessage
from config import EMAIL_HOST, EMAIL_PASSWORD, EMAIL_PORT, EMAIL_RECIPIENT, EMAIL_SENDER
class EmailNotifier:
def __init__(self):
self.smtp_server = EMAIL_HOST
self.port = EMAIL_PORT
self.sender = EMAIL_SENDER
self.password = EMAIL_PASSWORD
self.recipient = EMAIL_RECIPIENT
def send_email(self, subject, body, html=False):
try:
msg = EmailMessage()
msg["Subject"] = subject
msg["From"] = self.sender
msg["To"] = self.recipient
if html:
msg.add_alternative(body, subtype="html")
else:
msg.set_content(body)
with smtplib.SMTP(self.smtp_server, self.port) as server:
server.starttls()
server.login(self.sender, self.password)
server.send_message(msg)
logging.info("[EmailNotifier] Email sent.")
except Exception as e:
logging.error(f"[EmailNotifier] Failed to send email: {e}")