Vikas Nale 0bd733ecb8 - Handle GZIP in database backup process, so that all the backups are zipped before uploaded to s3
- Add fag to check if emails should be send on process complete like did for GIT and S3
2025-07-05 11:23:27 +05:30

36 lines
1015 B
Python

import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
load_dotenv()
EMAIL_HOST = os.getenv("EMAIL_HOST", "smtp.gmail.com")
EMAIL_PORT = int(os.getenv("EMAIL_PORT", 587))
EMAIL_USER = os.getenv("EMAIL_USER")
EMAIL_PASS = os.getenv("EMAIL_PASS")
def send_email(subject, body, to_emails, html=False):
if isinstance(to_emails, str):
to_emails = [to_emails]
msg = MIMEMultipart()
msg["From"] = EMAIL_USER
msg["To"] = ", ".join(to_emails)
msg["Subject"] = subject
if html:
msg.attach(MIMEText(body, "html"))
else:
msg.attach(MIMEText(body, "plain"))
try:
with smtplib.SMTP(EMAIL_HOST, EMAIL_PORT) as server:
server.starttls()
server.login(EMAIL_USER, EMAIL_PASS)
server.sendmail(EMAIL_USER, to_emails, msg.as_string())
return True
except Exception as e:
print(f"Error sending email: {e}")
return False