2024-05-29 19:06:16 +09:00
|
|
|
import logging
|
2023-07-14 11:19:26 +08:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from flask import Flask
|
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
from configs import dify_config
|
2024-11-30 23:05:22 +08:00
|
|
|
from dify_app import DifyApp
|
2025-06-19 15:00:42 +08:00
|
|
|
from libs.mail import MailConfigError, MailMessage, MailSender, MailSenderFactory
|
2024-10-22 11:01:32 +08:00
|
|
|
|
2023-07-14 11:19:26 +08:00
|
|
|
|
|
|
|
class Mail:
|
|
|
|
def __init__(self):
|
2025-06-19 15:00:42 +08:00
|
|
|
self._sender: Optional[MailSender] = None
|
2023-07-14 11:19:26 +08:00
|
|
|
|
|
|
|
def is_inited(self) -> bool:
|
2025-06-19 15:00:42 +08:00
|
|
|
return self._sender is not None
|
|
|
|
|
|
|
|
def init_app(self, app: Flask) -> None:
|
|
|
|
"""Initialize mail sender using the new factory pattern."""
|
|
|
|
try:
|
|
|
|
self._sender = MailSenderFactory.create_from_dify_config(dify_config)
|
|
|
|
if self._sender:
|
|
|
|
logging.info("Mail sender initialized successfully")
|
|
|
|
else:
|
|
|
|
logging.warning("MAIL_TYPE is not set, mail functionality disabled")
|
|
|
|
except MailConfigError as e:
|
|
|
|
logging.exception("Failed to initialize mail sender")
|
|
|
|
raise ValueError(f"Mail configuration error: {e}")
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Unexpected error initializing mail sender")
|
|
|
|
raise ValueError(f"Failed to initialize mail sender: {e}")
|
2023-07-14 11:19:26 +08:00
|
|
|
|
|
|
|
def send(self, to: str, subject: str, html: str, from_: Optional[str] = None):
|
2025-06-19 15:00:42 +08:00
|
|
|
"""
|
|
|
|
Send an email using the configured mail sender.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
to: Recipient email address
|
|
|
|
subject: Email subject
|
|
|
|
html: Email HTML content
|
|
|
|
from_: Sender email address (optional, uses default if not provided)
|
|
|
|
"""
|
|
|
|
if not self._sender:
|
|
|
|
raise ValueError("Mail sender is not initialized")
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Create mail message
|
|
|
|
message = MailMessage(to=to, subject=subject, html=html, from_=from_)
|
|
|
|
|
|
|
|
# Send the message
|
|
|
|
self._sender.send(message)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(f"Failed to send email to {to}")
|
|
|
|
raise
|
2023-07-14 11:19:26 +08:00
|
|
|
|
|
|
|
|
2024-11-30 23:05:22 +08:00
|
|
|
def is_enabled() -> bool:
|
|
|
|
return dify_config.MAIL_TYPE is not None and dify_config.MAIL_TYPE != ""
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app: DifyApp):
|
2023-07-14 11:19:26 +08:00
|
|
|
mail.init_app(app)
|
|
|
|
|
|
|
|
|
|
|
|
mail = Mail()
|