feat(交易记录): 新增交易记录管理页面与API接口 feat(上传): 添加统一上传接口支持自动识别文件类型 feat(用户管理): 为用户模型添加备注字段并更新相关接口 feat(邮件): 实现SMTP邮件发送功能并添加测试脚本 feat(短信): 增强短信服务配置灵活性与日志记录 fix(发票): 修复发票列表时间筛选功能 fix(nginx): 调整上传大小限制与超时配置 docs: 添加多个功能模块的说明文档 docs(估值): 补充估值计算流程与API提交数据说明 chore: 更新依赖与Docker镜像版本
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
import smtplib
|
|
from email.mime.base import MIMEBase
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
from email import encoders
|
|
from typing import Optional
|
|
import httpx
|
|
|
|
from app.settings.config import settings
|
|
|
|
|
|
class EmailClient:
|
|
def send(self, to_email: str, subject: Optional[str], body: str, file_bytes: Optional[bytes], file_name: Optional[str], content_type: Optional[str]) -> dict:
|
|
if not settings.SMTP_HOST or not settings.SMTP_PORT or not settings.SMTP_FROM:
|
|
raise RuntimeError("SMTP 未配置")
|
|
|
|
msg = MIMEMultipart()
|
|
msg["From"] = settings.SMTP_FROM
|
|
msg["To"] = to_email
|
|
msg["Subject"] = subject or "估值服务通知"
|
|
msg.attach(MIMEText(body, "plain", "utf-8"))
|
|
|
|
if file_bytes and file_name:
|
|
part = MIMEBase("application", "octet-stream")
|
|
part.set_payload(file_bytes)
|
|
encoders.encode_base64(part)
|
|
part.add_header("Content-Disposition", f"attachment; filename=\"{file_name}\"")
|
|
msg.attach(part)
|
|
|
|
if settings.SMTP_TLS:
|
|
server = smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT, timeout=30)
|
|
server.starttls()
|
|
else:
|
|
server = smtplib.SMTP_SSL(settings.SMTP_HOST, settings.SMTP_PORT, timeout=30)
|
|
try:
|
|
if settings.SMTP_USERNAME and settings.SMTP_PASSWORD:
|
|
server.login(settings.SMTP_USERNAME, settings.SMTP_PASSWORD)
|
|
server.sendmail(settings.SMTP_FROM, [to_email], msg.as_string())
|
|
server.quit()
|
|
return {"status": "OK"}
|
|
except Exception as e:
|
|
try:
|
|
server.quit()
|
|
except Exception:
|
|
pass
|
|
return {"status": "FAIL", "error": str(e)}
|
|
|
|
|
|
email_client = EmailClient() |