import smtplib
from email.message import EmailMessage





class SMTPTransporter:
    def __init__(self, host, port, username, password, use_ssl=True):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.use_ssl = use_ssl
        self.server = None

    def connect(self):
        if self.use_ssl:
            self.server = smtplib.SMTP_SSL(self.host, self.port)
        else:
            self.server = smtplib.SMTP(self.host, self.port)
            self.server.starttls()
        self.server.login(self.username, self.password)

    def send(self, to_email, subject, body, from_email=None):
        if not self.server:
            self.connect()
        
        msg = EmailMessage()
        msg["Subject"] = subject
        msg["From"] = from_email or self.username
        msg["To"] = to_email
        msg.add_alternative(body, subtype="html")
        self.server.send_message(msg)

    def close(self):
        if self.server:
            self.server.quit()
            self.server = None

    def disconnect(self):
        self.close()  # alias for semantic clarity

# Example usage:
transporter = SMTPTransporter(
    host="smtp.gmail.com",
    port=465,
    # username="javvajivenugopalrao@gmail.com",
    # password="gvbiczxuefdktlqp"
    username="venkatesh@mavengroup.in",
    password="krdzqwbtfmqknnqm"
  
)


