# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/07_notifiy.ipynb.

# %% auto 0
__all__ = ['send_email']

# %% ../nbs/07_notifiy.ipynb 1
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from .config import fetch_env_var

# %% ../nbs/07_notifiy.ipynb 2
def send_email(subject: str, body: str) -> None:
    # Set up your email details
    smtp_server = fetch_env_var("EMAIL_SERVER")
    smtp_port = 587
    email = fetch_env_var("EMAIL_USERNAME")
    password = fetch_env_var("EMAIL_PASSWORD")
    to_email = fetch_env_var("EMAIL_RECIPIENT")

    # Create a MIME multipart message
    msg = MIMEMultipart()
    msg['From'] = email
    msg['To'] = to_email
    msg['Subject'] = subject

    # Body of the email
    msg.attach(MIMEText(body, 'plain'))

    try:
        # Establish a secure session
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()

        # Login to your email account
        server.login(email, password)

        # Send the email
        text = msg.as_string()
        server.sendmail(email, to_email, text)

        # Close the connection
        server.quit()

        print('Email sent successfully.')

    except Exception as e:
        print('Failed to send email: ' + str(e))
