from app import app
from flask_mail import Message
from flask_mail import Mail

from app.config.settings import Config


class MailService:
    @staticmethod
    def send_email(subject, recipient, message, attachment=None, html=False): 
        try:
            mail = Mail(app)
            msg = Message(
                subject, 
                sender=Config.MAIL_DEFAULT_SENDER, 
                recipients=[recipient]
            )
            if html:
                msg.html = message
            else:
                msg.body = message
            # if attachment:
            #     with app.open_resource(attachment) as fp:
            #         msg.attach(attachment, 'application/octect-stream', fp.read())

            mail.send(msg)
            return True
        except Exception as e:
            print(e.args)
            print(f"Failed to send email: {str(e)}")
            return False