mirror of https://github.com/langgenius/dify.git
feat: add inner mail api
This commit is contained in:
parent
7f63cd52a2
commit
e38631db8a
|
@ -6,3 +6,4 @@ bp = Blueprint("inner_api", __name__, url_prefix="/inner/api")
|
|||
api = ExternalApi(bp)
|
||||
|
||||
from .workspace import workspace
|
||||
import mail
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
from flask_restful import Resource # type: ignore
|
||||
from flask_restful import reqparse
|
||||
|
||||
from controllers.console.wraps import setup_required
|
||||
from controllers.inner_api import api
|
||||
from controllers.inner_api.wraps import inner_api_only
|
||||
from services.enterprise.mail_service import DifyMail, EnterpriseMailService
|
||||
|
||||
|
||||
class EnterpriseMail(Resource):
|
||||
@setup_required
|
||||
@inner_api_only
|
||||
def post(self):
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument("to", type=str, required=True)
|
||||
parser.add_argument("subject", type=str, required=True)
|
||||
parser.add_argument("body", type=str, required=True)
|
||||
parser.add_argument("substitutions", type=dict, required=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
EnterpriseMailService.send_mail(DifyMail(**args))
|
||||
return {"message": "success"}, 200
|
||||
|
||||
|
||||
api.add_resource(EnterpriseMail, "/enterprise/mail")
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
from typing import Dict, List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from tasks.mail_enterprise_task import send_enterprise_email_task
|
||||
|
||||
|
||||
class DifyMail(BaseModel):
|
||||
to: List[str]
|
||||
subject: str
|
||||
body: str
|
||||
substitutions: Dict[str, str] = {}
|
||||
|
||||
|
||||
class EnterpriseMailService:
|
||||
|
||||
@classmethod
|
||||
def send_mail(cls, mail: DifyMail):
|
||||
|
||||
send_enterprise_email_task.delay(
|
||||
to=mail.to,
|
||||
subject=mail.subject,
|
||||
body=mail.body,
|
||||
substitutions=mail.substitutions
|
||||
)
|
|
@ -0,0 +1,37 @@
|
|||
import logging
|
||||
import time
|
||||
|
||||
import click
|
||||
from celery import shared_task # type: ignore
|
||||
from flask import render_template_string
|
||||
|
||||
from extensions.ext_mail import mail
|
||||
|
||||
|
||||
@shared_task(queue="mail")
|
||||
def send_enterprise_email_task(to, subject, body, substitutions):
|
||||
if not mail.is_inited():
|
||||
return
|
||||
|
||||
logging.info(
|
||||
click.style("Start enterprise mail to {} with subject {}".format(to, subject), fg="green")
|
||||
)
|
||||
start_at = time.perf_counter()
|
||||
|
||||
try:
|
||||
html_content = render_template_string(body, **substitutions)
|
||||
|
||||
if isinstance(to, list):
|
||||
for t in to:
|
||||
mail.send(to=t, subject=subject, html=html_content)
|
||||
else:
|
||||
mail.send(to=to, subject=subject, html=html_content)
|
||||
|
||||
end_at = time.perf_counter()
|
||||
logging.info(
|
||||
click.style(
|
||||
"Send enterprise mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green"
|
||||
)
|
||||
)
|
||||
except Exception:
|
||||
logging.exception("Send enterprise mail to {} failed".format(to))
|
Loading…
Reference in New Issue