import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from decouple import config


def send_email(subject, sender_email, recipient_email, body):
    # Email server settings (e.g., for Gmail)
    smtp_server = config('MAIL_SERVER')  # Replace with your SMTP server
    smtp_port = config('MAIL_PORT')  # Replace with the appropriate port (e.g., 587 for TLS)

    # Sender's email and password (use an App Password for Gmail)
    sender_password = config('MAIL_PASSWORD')  # Replace with your sender's password

    # Create a message object
    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = str(recipient_email)
    msg['Subject'] = subject

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

    try:
        # Create an SMTP session
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()
        
        # Log in to the email server
        server.login(sender_email, sender_password)

        # Send the email
        server.sendmail(sender_email, recipient_email, msg.as_string())
        
        # Quit the SMTP server
        server.quit()

        print("Email sent successfully!")
    except Exception as e:
        print("Error sending email:", str(e))
