diff --git a/backend/plugin/email/plugin.toml b/backend/plugin/email/plugin.toml index 20162fd7..e36cdbf3 100644 --- a/backend/plugin/email/plugin.toml +++ b/backend/plugin/email/plugin.toml @@ -1,6 +1,6 @@ [plugin] summary = '电子邮件' -version = '0.0.1' +version = '0.0.2' description = '发送电子邮件,例如验证码、通知等' author = 'wu-clan' diff --git a/backend/plugin/email/templates/captcha.html b/backend/plugin/email/templates/captcha.html index 35acb457..0a89ef0e 100644 --- a/backend/plugin/email/templates/captcha.html +++ b/backend/plugin/email/templates/captcha.html @@ -60,8 +60,8 @@
您好,您正在进行绑定操作,请使用以下验证码:
+您正在进行绑定操作,请使用以下验证码:
{{code}}验证码有效期为 {{expired}}分钟,请勿泄露给他人。
diff --git a/backend/plugin/email/utils/send.py b/backend/plugin/email/utils/send.py index 03353071..424fc309 100644 --- a/backend/plugin/email/utils/send.py +++ b/backend/plugin/email/utils/send.py @@ -12,6 +12,7 @@ from sqlalchemy import inspect from sqlalchemy.ext.asyncio import AsyncSession +from backend.common.enums import StatusType from backend.common.exception import errors from backend.common.log import log from backend.core.conf import settings @@ -66,46 +67,52 @@ async def send_email( :param template: 邮件内容模板 :return: """ + # 本地配置 + email_host = settings.EMAIL_HOST + email_port = settings.EMAIL_PORT + email_ssl = settings.EMAIL_SSL + email_username = settings.EMAIL_USERNAME + email_password = settings.EMAIL_PASSWORD + # 动态配置 - dynamic_email_config = None + dynamic_config = None - # 检查 config 插件配置 def get_config_table(conn): inspector = inspect(conn) return inspector.has_table('sys_config', schema=None) async with async_engine.begin() as coon: exists = await coon.run_sync(get_config_table) - - if exists: - dynamic_email_config = await config_dao.get_by_type(db, 'email') + if exists: + dynamic_config = await config_dao.get_all(db, 'EMAIL') + + if dynamic_config: + _status_key = 'EMAIL_STATUS' + _host_key = 'EMAIL_HOST' + _port_key = 'EMAIL_PORT' + _ssl_key = 'EMAIL_SSL' + _username_key = 'EMAIL_USERNAME' + _password_key = 'EMAIL_PASSWORD' + + configs = {d['key']: d['value'] for d in select_list_serialize(dynamic_config)} + if configs.get(_status_key): + if len(dynamic_config) < 6: + raise errors.NotFoundError(msg='缺少邮件动态配置,请检查系统参数配置-邮件配置') + email_host = configs.get(_host_key) + email_port = int(configs.get(_port_key, 0)) + email_ssl = True if configs.get(_ssl_key, '') == str(StatusType.enable.value) else False + email_username = configs.get(_username_key) + email_password = configs.get(_password_key) try: - # 动态配置发送 - if dynamic_email_config: - configs = {d['key']: d for d in select_list_serialize(dynamic_email_config)} - if configs.get('EMAIL_STATUS'): - if len(dynamic_email_config) < 6: - raise errors.NotFoundError(msg='缺少邮件动态配置,请检查系统参数配置-邮件配置') - smtp_client = SMTP( - hostname=configs.get('EMAIL_HOST'), - port=configs.get('EMAIL_PORT'), - use_tls=configs.get('EMAIL_SSL') == '1', - ) - message = await render_message(subject, configs.get('EMAIL_USERNAME'), content, template) # type: ignore - async with smtp_client: - await smtp_client.login(configs.get('EMAIL_USERNAME'), configs.get('EMAIL_PASSWORD')) # type: ignore - await smtp_client.sendmail(configs.get('EMAIL_USERNAME'), recipients, message) # type: ignore - - # 本地配置发送 - message = await render_message(subject, settings.EMAIL_USERNAME, content, template) + message = await render_message(subject, email_username, content, template) smtp_client = SMTP( - hostname=settings.EMAIL_HOST, - port=settings.EMAIL_PORT, - use_tls=settings.EMAIL_SSL, + hostname=email_host, + port=email_port, + use_tls=email_ssl, ) async with smtp_client: - await smtp_client.login(settings.EMAIL_USERNAME, settings.EMAIL_PASSWORD) - await smtp_client.sendmail(settings.EMAIL_USERNAME, recipients, message) + await smtp_client.login(email_username, email_password) + await smtp_client.sendmail(email_username, recipients, message) except Exception as e: log.error(f'电子邮件发送失败:{e}')