from email.mime.multipart import MIMEMultipart
# from passlib.hash import pbkdf2_sha256  # for password encrytipn
from email.mime.text import MIMEText
from email.utils import formataddr
import ssl
import smtplib


def otpMail(otp, rec_mail):
    sender_email = "no-reply@kanzartar.iconicmyanmar.com"
    receiver_email = rec_mail
    smtp_server = "kanzartar.iconicmyanmar.com"
    password = "Octimu12!"

    message = MIMEMultipart("alternative")
    message[
        "Subject"] = f"Your OTP code for NEW ACCOUNT Sign Up - {otp}"
    message["From"] = formataddr(('Kan Zartar', sender_email))
    message["To"] = receiver_email

    # Create the plain-text and HTML version of your message
    text = f"""\
    Your code is: {otp}. Use it to verify your email and activate two-step verification in Kan Zartar App.

    If you didn't request this, simply ignore this message.

    Yours,
    Kan Zartar App Team
    """
    html = f"""\
    <body style="font-family: 'PT Sans', sans-serif;">
    <p>Your code is: {otp}. Use it to verify your email and activate two-step verification in Kan Zartar App.</p>
    <p style="margin: 50px 0px;">If you didn't request this, simply ignore this message.</p>

    <p>Yours, <br>
        Kan Zartar App Team<br>
    </p>
    </body>
    """

    # Turn these into plain/html MIMEText objects
    part1 = MIMEText(text, "plain")
    part2 = MIMEText(html, "html")

    # Add HTML/plain-text parts to MIMEMultipart message
    # The email client will try to render the last part first
    message.attach(part1)
    message.attach(part2)

    # Create secure connection with server and send email
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL(smtp_server, 465, context=context) as server:
        server.login(sender_email, password)
        server.sendmail(
            sender_email, receiver_email, message.as_string()
        )


# otpMail('123456', 'phyowaiyantungamc@gmail.com')
