mirror of
https://github.com/zhinianboke/xianyu-auto-reply.git
synced 2025-08-30 01:27:35 +08:00
增加自定义SMTP功能-新增SMTP相关系统设置键
新增SMTP相关系统设置键
This commit is contained in:
parent
dea041a977
commit
461cd46e02
@ -396,7 +396,14 @@ class DBManager:
|
|||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT OR IGNORE INTO system_settings (key, value, description) VALUES
|
INSERT OR IGNORE INTO system_settings (key, value, description) VALUES
|
||||||
('theme_color', 'blue', '主题颜色'),
|
('theme_color', 'blue', '主题颜色'),
|
||||||
('registration_enabled', 'true', '是否开启用户注册')
|
('registration_enabled', 'true', '是否开启用户注册'),
|
||||||
|
('smtp_server', '', 'SMTP服务器地址'),
|
||||||
|
('smtp_port', '587', 'SMTP端口'),
|
||||||
|
('smtp_user', '', 'SMTP登录用户名(发件邮箱)'),
|
||||||
|
('smtp_password', '', 'SMTP登录密码/授权码'),
|
||||||
|
('smtp_from', '', '发件人显示名(留空则使用用户名)'),
|
||||||
|
('smtp_use_tls', 'true', '是否启用TLS'),
|
||||||
|
('smtp_use_ssl', 'false', '是否启用SSL')
|
||||||
''')
|
''')
|
||||||
|
|
||||||
# 检查并升级数据库
|
# 检查并升级数据库
|
||||||
@ -2470,7 +2477,7 @@ class DBManager:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
async def send_verification_email(self, email: str, code: str) -> bool:
|
async def send_verification_email(self, email: str, code: str) -> bool:
|
||||||
"""发送验证码邮件"""
|
"""发送验证码邮件(仅SMTP)"""
|
||||||
try:
|
try:
|
||||||
subject = "闲鱼自动回复系统 - 邮箱验证码"
|
subject = "闲鱼自动回复系统 - 邮箱验证码"
|
||||||
# 使用简单的纯文本邮件内容
|
# 使用简单的纯文本邮件内容
|
||||||
@ -2495,30 +2502,56 @@ class DBManager:
|
|||||||
此邮件由系统自动发送,请勿直接回复
|
此邮件由系统自动发送,请勿直接回复
|
||||||
© 2025 闲鱼自动回复系统"""
|
© 2025 闲鱼自动回复系统"""
|
||||||
|
|
||||||
# 使用GET请求发送邮件
|
# 从系统设置读取SMTP配置
|
||||||
api_url = "https://dy.zhinianboke.com/api/emailSend"
|
try:
|
||||||
params = {
|
smtp_server = self.get_system_setting('smtp_server') or ''
|
||||||
'subject': subject,
|
smtp_port = int(self.get_system_setting('smtp_port') or 0)
|
||||||
'receiveUser': email,
|
smtp_user = self.get_system_setting('smtp_user') or ''
|
||||||
'sendHtml': text_content
|
smtp_password = self.get_system_setting('smtp_password') or ''
|
||||||
}
|
smtp_from = (self.get_system_setting('smtp_from') or '').strip() or smtp_user
|
||||||
|
smtp_use_tls = (self.get_system_setting('smtp_use_tls') or 'true').lower() == 'true'
|
||||||
|
smtp_use_ssl = (self.get_system_setting('smtp_use_ssl') or 'false').lower() == 'true'
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"读取SMTP系统设置失败: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
# 校验配置完整性
|
||||||
try:
|
if not (smtp_server and smtp_port and smtp_user and smtp_password):
|
||||||
logger.info(f"发送验证码邮件: {email}")
|
logger.error("SMTP配置不完整,无法发送验证码邮件")
|
||||||
async with session.get(api_url, params=params, timeout=15) as response:
|
return False
|
||||||
response_text = await response.text()
|
|
||||||
logger.info(f"邮件API响应: {response.status}")
|
|
||||||
|
|
||||||
if response.status == 200:
|
# 使用SMTP方式发送
|
||||||
logger.info(f"验证码邮件发送成功: {email}")
|
try:
|
||||||
return True
|
import smtplib
|
||||||
else:
|
from email.mime.text import MIMEText
|
||||||
logger.error(f"验证码邮件发送失败: {email}, 状态码: {response.status}, 响应: {response_text[:200]}")
|
from email.mime.multipart import MIMEMultipart
|
||||||
return False
|
|
||||||
except Exception as e:
|
msg = MIMEMultipart()
|
||||||
logger.error(f"邮件发送异常: {email}, 错误: {e}")
|
msg['Subject'] = subject
|
||||||
return False
|
msg['From'] = smtp_from
|
||||||
|
msg['To'] = email
|
||||||
|
|
||||||
|
msg.attach(MIMEText(text_content, 'plain', 'utf-8'))
|
||||||
|
|
||||||
|
if smtp_use_ssl:
|
||||||
|
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
|
||||||
|
else:
|
||||||
|
server = smtplib.SMTP(smtp_server, smtp_port)
|
||||||
|
|
||||||
|
server.ehlo()
|
||||||
|
if smtp_use_tls and not smtp_use_ssl:
|
||||||
|
server.starttls()
|
||||||
|
server.ehlo()
|
||||||
|
|
||||||
|
server.login(smtp_user, smtp_password)
|
||||||
|
server.sendmail(smtp_user, [email], msg.as_string())
|
||||||
|
server.quit()
|
||||||
|
|
||||||
|
logger.info(f"验证码邮件发送成功(SMTP): {email}")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"SMTP发送验证码邮件失败: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"发送验证码邮件异常: {e}")
|
logger.error(f"发送验证码邮件异常: {e}")
|
||||||
@ -4466,4 +4499,4 @@ db_manager = DBManager()
|
|||||||
|
|
||||||
# 确保进程结束时关闭数据库连接
|
# 确保进程结束时关闭数据库连接
|
||||||
import atexit
|
import atexit
|
||||||
atexit.register(db_manager.close)
|
atexit.register(db_manager.close)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user