from odk_mailer.lib import utils,smtp
from odk_mailer.classes.Config import Config
from email.message import EmailMessage
import os

def send(eval):

    NOTIFY_TO = os.getenv('NOTIFY_TO')
    NOTIFY_FROM = os.getenv('NOTIFY_FROM')

    if not NOTIFY_FROM or not NOTIFY_TO:
        return

    smtp_config = Config(smtpRequired=True).get_smpt_config()

    subject = f"[ODK-Mailer] Mailjob {eval['hash'][:10]} succesful"

    message =   "ODK-Mailer has succesfully ran a scheduled mailjob:\n"\
                f"Job ID: {eval['hash']}\n"\
                f"Note: {eval['note']}\n"\
                f"Recipients: {eval['recipients']}\n"\
                f"Created: {eval['created']}\n"\
                f"Scheduled: {eval['scheduled']}\n"\
                f"Sent: {str(utils.now())}\n\n"\
                f"Host: {os.uname()[1]}\n\n"\
                "You are getting this email, because your address has been added to the ODK-Mailer notifaction list.\n"\
                "If you would like to stop receiving these messages, remove your address from NOTIFY_TO environment variable."                        

    email = EmailMessage()
    email['From'] = NOTIFY_FROM
    email['To'] = NOTIFY_TO
    email['Subject'] = subject
    email.set_content(message, subtype="plain")

    smtp.send(email, False, smtp_config)