mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Optimize email sending config update logic (#775)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[plugin]
|
||||
summary = '电子邮件'
|
||||
version = '0.0.1'
|
||||
version = '0.0.2'
|
||||
description = '发送电子邮件,例如验证码、通知等'
|
||||
author = 'wu-clan'
|
||||
|
||||
|
||||
@@ -60,8 +60,8 @@
|
||||
<tr>
|
||||
<td align="center" style="padding: 40px 0;">
|
||||
<div class="container">
|
||||
<div class="title">验证码</div>
|
||||
<p style="margin-top:20px;">您好,您正在进行绑定操作,请使用以下验证码:</p>
|
||||
<div class="title">FBA</div>
|
||||
<p style="margin-top:20px;">您正在进行绑定操作,请使用以下验证码:</p>
|
||||
<span class="code">{{code}}</span>
|
||||
<p class="tips">验证码有效期为 <strong>{{expired}}分钟</strong>,请勿泄露给他人。</p>
|
||||
<p class="tips">
|
||||
|
||||
@@ -12,6 +12,7 @@ from jinja2 import Template
|
||||
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_config = await config_dao.get_all(db, 'EMAIL')
|
||||
|
||||
if exists:
|
||||
dynamic_email_config = await config_dao.get_by_type(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}')
|
||||
|
||||
Reference in New Issue
Block a user