|
12 | 12 | from sqlalchemy import inspect |
13 | 13 | from sqlalchemy.ext.asyncio import AsyncSession |
14 | 14 |
|
| 15 | +from backend.common.enums import StatusType |
15 | 16 | from backend.common.exception import errors |
16 | 17 | from backend.common.log import log |
17 | 18 | from backend.core.conf import settings |
@@ -66,46 +67,52 @@ async def send_email( |
66 | 67 | :param template: 邮件内容模板 |
67 | 68 | :return: |
68 | 69 | """ |
| 70 | + # 本地配置 |
| 71 | + email_host = settings.EMAIL_HOST |
| 72 | + email_port = settings.EMAIL_PORT |
| 73 | + email_ssl = settings.EMAIL_SSL |
| 74 | + email_username = settings.EMAIL_USERNAME |
| 75 | + email_password = settings.EMAIL_PASSWORD |
| 76 | + |
69 | 77 | # 动态配置 |
70 | | - dynamic_email_config = None |
| 78 | + dynamic_config = None |
71 | 79 |
|
72 | | - # 检查 config 插件配置 |
73 | 80 | def get_config_table(conn): |
74 | 81 | inspector = inspect(conn) |
75 | 82 | return inspector.has_table('sys_config', schema=None) |
76 | 83 |
|
77 | 84 | async with async_engine.begin() as coon: |
78 | 85 | exists = await coon.run_sync(get_config_table) |
79 | | - |
80 | | - if exists: |
81 | | - dynamic_email_config = await config_dao.get_by_type(db, 'email') |
| 86 | + if exists: |
| 87 | + dynamic_config = await config_dao.get_all(db, 'EMAIL') |
| 88 | + |
| 89 | + if dynamic_config: |
| 90 | + _status_key = 'EMAIL_STATUS' |
| 91 | + _host_key = 'EMAIL_HOST' |
| 92 | + _port_key = 'EMAIL_PORT' |
| 93 | + _ssl_key = 'EMAIL_SSL' |
| 94 | + _username_key = 'EMAIL_USERNAME' |
| 95 | + _password_key = 'EMAIL_PASSWORD' |
| 96 | + |
| 97 | + configs = {d['key']: d['value'] for d in select_list_serialize(dynamic_config)} |
| 98 | + if configs.get(_status_key): |
| 99 | + if len(dynamic_config) < 6: |
| 100 | + raise errors.NotFoundError(msg='缺少邮件动态配置,请检查系统参数配置-邮件配置') |
| 101 | + email_host = configs.get(_host_key) |
| 102 | + email_port = int(configs.get(_port_key, 0)) |
| 103 | + email_ssl = True if configs.get(_ssl_key, '') == str(StatusType.enable.value) else False |
| 104 | + email_username = configs.get(_username_key) |
| 105 | + email_password = configs.get(_password_key) |
82 | 106 |
|
83 | 107 | try: |
84 | | - # 动态配置发送 |
85 | | - if dynamic_email_config: |
86 | | - configs = {d['key']: d for d in select_list_serialize(dynamic_email_config)} |
87 | | - if configs.get('EMAIL_STATUS'): |
88 | | - if len(dynamic_email_config) < 6: |
89 | | - raise errors.NotFoundError(msg='缺少邮件动态配置,请检查系统参数配置-邮件配置') |
90 | | - smtp_client = SMTP( |
91 | | - hostname=configs.get('EMAIL_HOST'), |
92 | | - port=configs.get('EMAIL_PORT'), |
93 | | - use_tls=configs.get('EMAIL_SSL') == '1', |
94 | | - ) |
95 | | - message = await render_message(subject, configs.get('EMAIL_USERNAME'), content, template) # type: ignore |
96 | | - async with smtp_client: |
97 | | - await smtp_client.login(configs.get('EMAIL_USERNAME'), configs.get('EMAIL_PASSWORD')) # type: ignore |
98 | | - await smtp_client.sendmail(configs.get('EMAIL_USERNAME'), recipients, message) # type: ignore |
99 | | - |
100 | | - # 本地配置发送 |
101 | | - message = await render_message(subject, settings.EMAIL_USERNAME, content, template) |
| 108 | + message = await render_message(subject, email_username, content, template) |
102 | 109 | smtp_client = SMTP( |
103 | | - hostname=settings.EMAIL_HOST, |
104 | | - port=settings.EMAIL_PORT, |
105 | | - use_tls=settings.EMAIL_SSL, |
| 110 | + hostname=email_host, |
| 111 | + port=email_port, |
| 112 | + use_tls=email_ssl, |
106 | 113 | ) |
107 | 114 | async with smtp_client: |
108 | | - await smtp_client.login(settings.EMAIL_USERNAME, settings.EMAIL_PASSWORD) |
109 | | - await smtp_client.sendmail(settings.EMAIL_USERNAME, recipients, message) |
| 115 | + await smtp_client.login(email_username, email_password) |
| 116 | + await smtp_client.sendmail(email_username, recipients, message) |
110 | 117 | except Exception as e: |
111 | 118 | log.error(f'电子邮件发送失败:{e}') |
0 commit comments