# Path: irisett/log.py
# def configure_logging(logtype: str, logfilename: Optional[str]=None, debug_logging: bool=False,
#                       rotate_length: int=1000000, max_rotated_files: int=250) -> None:
# def msg(logmsg: str, section: Optional[str]=None) -> None:
# def debug(logmsg: str, section: Optional[str]=None) -> None:
#     def log_msg(self, logmsg: str) -> None:
#     def log_debug(self, logmsg: str) -> None:
# class LoggingMixin:


from typing import Optional, Dict, Any, Union, Iterable
from email import charset
from email.mime.text import MIMEText
from irisett import (
    log,
)
import aiosmtplib
import jinja2
import asyncio
"""Send notification emails."""

charset.add_charset('utf-8', charset.SHORTEST, charset.QP)  # type: ignore
# noinspection PyPep8

# noinspection PyPep8


async def send_email(loop: asyncio.AbstractEventLoop, mail_from: str, mail_to: Union[Iterable, str],
                     subject: str, body: str, server: str='localhost') -> None:
    """Send an email to one or more recipients.

    Only supports plain text emails with a single message body.
    No attachments etc.
    """
    if type(mail_to) == str:
        mail_to = [mail_to]
    smtp = aiosmtplib.SMTP(hostname=server, port=25, loop=loop)
    try:
        await smtp.connect()
        for rcpt in mail_to:
            msg = MIMEText(body)
            msg['Subject'] = subject
            msg['From'] = mail_from
            msg['To'] = rcpt
            await smtp.send_message(msg)
        await smtp.quit()
    except aiosmtplib.errors.SMTPException as e:

# Path: irisett/log.py
# def configure_logging(logtype: str, logfilename: Optional[str]=None, debug_logging: bool=False,
#                       rotate_length: int=1000000, max_rotated_files: int=250) -> None:
# def msg(logmsg: str, section: Optional[str]=None) -> None:
# def debug(logmsg: str, section: Optional[str]=None) -> None:
#     def log_msg(self, logmsg: str) -> None:
#     def log_debug(self, logmsg: str) -> None:
# class LoggingMixin:


# Path: irisett/notify/email.py
from typing import Optional, Dict, Any, Union, Iterable
from email import charset
from email.mime.text import MIMEText
from irisett import (
    log,
)
import aiosmtplib
import jinja2
import asyncio

"""Send notification emails."""

charset.add_charset('utf-8', charset.SHORTEST, charset.QP)  # type: ignore
# noinspection PyPep8

# noinspection PyPep8


async def send_email(loop: asyncio.AbstractEventLoop, mail_from: str, mail_to: Union[Iterable, str],
                     subject: str, body: str, server: str='localhost') -> None:
    """Send an email to one or more recipients.

    Only supports plain text emails with a single message body.
    No attachments etc.
    """
    if type(mail_to) == str:
        mail_to = [mail_to]
    smtp = aiosmtplib.SMTP(hostname=server, port=25, loop=loop)
    try:
        await smtp.connect()
        for rcpt in mail_to:
            msg = MIMEText(body)
            msg['Subject'] = subject
            msg['From'] = mail_from
            msg['To'] = rcpt
            await smtp.send_message(msg)
        await smtp.quit()
    except aiosmtplib.errors.SMTPException as e:

        log.msg('Error sending smtp notification: %s' % (str(e)), 'NOTIFICATIONS')