Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
ext_mail.py92 linesDownload Raw Back to extensions
1import logging2from typing import Optional3 4import resend5from flask import Flask6 7from configs import dify_config8 9 10class Mail:11    def __init__(self):12        self._client = None13        self._default_send_from = None14 15    def is_inited(self) -> bool:16        return self._client is not None17 18    def init_app(self, app: Flask):19        mail_type = dify_config.MAIL_TYPE20        if not mail_type:21            logging.warning("MAIL_TYPE is not set")22            return23 24        if dify_config.MAIL_DEFAULT_SEND_FROM:25            self._default_send_from = dify_config.MAIL_DEFAULT_SEND_FROM26 27        match mail_type:28            case "resend":29                api_key = dify_config.RESEND_API_KEY30                if not api_key:31                    raise ValueError("RESEND_API_KEY is not set")32 33                api_url = dify_config.RESEND_API_URL34                if api_url:35                    resend.api_url = api_url36 37                resend.api_key = api_key38                self._client = resend.Emails39            case "smtp":40                from libs.smtp import SMTPClient41 42                if not dify_config.SMTP_SERVER or not dify_config.SMTP_PORT:43                    raise ValueError("SMTP_SERVER and SMTP_PORT are required for smtp mail type")44                if not dify_config.SMTP_USE_TLS and dify_config.SMTP_OPPORTUNISTIC_TLS:45                    raise ValueError("SMTP_OPPORTUNISTIC_TLS is not supported without enabling SMTP_USE_TLS")46                self._client = SMTPClient(47                    server=dify_config.SMTP_SERVER,48                    port=dify_config.SMTP_PORT,49                    username=dify_config.SMTP_USERNAME,50                    password=dify_config.SMTP_PASSWORD,51                    _from=dify_config.MAIL_DEFAULT_SEND_FROM,52                    use_tls=dify_config.SMTP_USE_TLS,53                    opportunistic_tls=dify_config.SMTP_OPPORTUNISTIC_TLS,54                )55            case _:56                raise ValueError("Unsupported mail type {}".format(mail_type))57 58    def send(self, to: str, subject: str, html: str, from_: Optional[str] = None):59        if not self._client:60            raise ValueError("Mail client is not initialized")61 62        if not from_ and self._default_send_from:63            from_ = self._default_send_from64 65        if not from_:66            raise ValueError("mail from is not set")67 68        if not to:69            raise ValueError("mail to is not set")70 71        if not subject:72            raise ValueError("mail subject is not set")73 74        if not html:75            raise ValueError("mail html is not set")76 77        self._client.send(78            {79                "from": from_,80                "to": to,81                "subject": subject,82                "html": html,83            }84        )85 86 87def init_app(app: Flask):88    mail.init_app(app)89 90 91mail = Mail()92