Files
PerToolBoxServer/backend/utils/email.py
2026-03-31 16:03:55 +08:00

52 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PerToolBox Server - 腾讯企业邮箱服务
Copyright (C) 2024 Sea Network Technology Studio
Author: Canglan <admin@sea-studio.top>
License: AGPL v3
"""
import logging
from typing import Optional
from ..config import settings
logger = logging.getLogger(__name__)
async def send_email(recipient: str, code: str) -> bool:
"""发送邮件验证码(腾讯企业邮)"""
if not all([
settings.SMTP_USER,
settings.SMTP_PASSWORD,
settings.SMTP_FROM
]):
logger.warning("邮件服务未配置,使用模拟验证码")
return True
try:
import aiosmtplib
from email.mime.text import MIMEText
subject = "PerToolBox 验证码"
body = f"您的验证码是:{code}5分钟内有效。"
msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = settings.SMTP_FROM
msg['To'] = recipient
await aiosmtplib.send(
msg,
hostname=settings.SMTP_HOST,
port=settings.SMTP_PORT,
username=settings.SMTP_USER,
password=settings.SMTP_PASSWORD,
use_tls=True
)
logger.info(f"邮件发送成功: {recipient}")
return True
except Exception as e:
logger.error(f"邮件发送异常: {e}")
return False