from dotenv import load_dotenv
import os
import smtplib
from email.mime.text import MIMEText
load_dotenv()

SMTP_SERVER=os.getenv('SMTP_SERVER')
SMTP_PORT=587
SMTP_USERNAME=os.getenv('SMTP_USERNAME')
SMTP_PASSWORD=os.getenv('SMTP_PASSWORD')
MAIL_USE_TLS = True
MAIL_USE_SSL = False
to_email=os.getenv('TO_EMAIL')
message_text=os.getenv('MESSAGE_TEXT')

def send_email(sitename):
    full_message_text = f"{message_text}\nSite Name: {sitename}"
    """ Send an email notification """
    msg = MIMEText(full_message_text)
    msg['Subject'] = 'New  Error Message:'
    msg['From'] = SMTP_USERNAME
    msg['To'] = to_email
    
    try:
        with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
            server.starttls()
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.send_message(msg)
    except Exception as e:
        raise ValueError(f"Failed to send email to {to_email}: {e}")