From 23810cf18083bb3d4129fdefd2a3417b68773a5e Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Mon, 1 Jun 2026 17:37:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=20sendmail=20+=20Date=E5=A4=B4=EF=BC=8C?= =?UTF-8?q?=E5=AF=B9=E9=BD=90=E5=B7=B2=E9=AA=8C=E8=AF=81=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - send_message() → sendmail()(与 send_email.py 一致) - 新增 Date 头(部分SMTP服务器缺少会拦截) - 增加发送前/后详细日志 --- routes_email.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/routes_email.py b/routes_email.py index 9f7c854..ba19583 100644 --- a/routes_email.py +++ b/routes_email.py @@ -35,26 +35,30 @@ def register_email_routes(app): smtp_user = os.environ.get('SMTP_USER', 'hz4th_coder@tphai.com') smtp_pass = os.environ.get('SMTP_PASS', 'hz4th_coder@!') - # 创建邮件 + log_message(f"📧 准备发送邮件: to={to_email}, subject={subject}, user={smtp_user}") + + # 创建邮件(完全对齐 send_email.py 的方式) + from email.utils import formataddr, formatdate msg = MIMEMultipart() - from email.utils import formataddr msg['From'] = formataddr(('黄庄4号程序员', smtp_user)) msg['To'] = to_email msg['Subject'] = subject + msg['Date'] = formatdate(localtime=True) # 添加正文 msg.attach(MIMEText(body, 'plain', 'utf-8')) - # 发送邮件(无SSL模式,需要先ehlo) + # 发送邮件(无SSL模式,完全对齐 send_email.py) server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.login(smtp_user, smtp_pass) - server.send_message(msg) + # 使用 sendmail 而非 send_message,与已验证成功的脚本一致 + server.sendmail(smtp_user, [to_email], msg.as_string()) server.quit() - log_message(f"邮件发送成功: {subject} -> {to_email}") + log_message(f"✅ 邮件发送成功: {subject} -> {to_email}") return jsonify({'message': '邮件发送成功'}) except Exception as e: - log_message(f"邮件发送失败: {e}") + log_message(f"❌ 邮件发送失败: {e}") return jsonify({'error': str(e)}), 500 \ No newline at end of file