Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23810cf180 | |||
| 16400b0359 |
@@ -32,27 +32,33 @@ def register_email_routes(app):
|
|||||||
# SMTP配置 (从环境变量或默认值)
|
# SMTP配置 (从环境变量或默认值)
|
||||||
smtp_host = os.environ.get('SMTP_HOST', 'mail.tphai.com')
|
smtp_host = os.environ.get('SMTP_HOST', 'mail.tphai.com')
|
||||||
smtp_port = int(os.environ.get('SMTP_PORT', 587))
|
smtp_port = int(os.environ.get('SMTP_PORT', 587))
|
||||||
smtp_user = os.environ.get('SMTP_USER', 'favor@tphai.com')
|
smtp_user = os.environ.get('SMTP_USER', 'hz4th_coder@tphai.com')
|
||||||
smtp_pass = os.environ.get('SMTP_PASS', 'favor@!')
|
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()
|
msg = MIMEMultipart()
|
||||||
msg['From'] = smtp_user
|
msg['From'] = formataddr(('黄庄4号程序员', smtp_user))
|
||||||
msg['To'] = to_email
|
msg['To'] = to_email
|
||||||
msg['Subject'] = subject
|
msg['Subject'] = subject
|
||||||
|
msg['Date'] = formatdate(localtime=True)
|
||||||
|
|
||||||
# 添加正文
|
# 添加正文
|
||||||
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
||||||
|
|
||||||
# 发送邮件
|
# 发送邮件(无SSL模式,完全对齐 send_email.py)
|
||||||
server = smtplib.SMTP(smtp_host, smtp_port)
|
server = smtplib.SMTP(smtp_host, smtp_port)
|
||||||
|
server.ehlo()
|
||||||
server.login(smtp_user, smtp_pass)
|
server.login(smtp_user, smtp_pass)
|
||||||
server.send_message(msg)
|
# 使用 sendmail 而非 send_message,与已验证成功的脚本一致
|
||||||
|
server.sendmail(smtp_user, [to_email], msg.as_string())
|
||||||
server.quit()
|
server.quit()
|
||||||
|
|
||||||
log_message(f"邮件发送成功: {subject} -> {to_email}")
|
log_message(f"✅ 邮件发送成功: {subject} -> {to_email}")
|
||||||
|
|
||||||
return jsonify({'message': '邮件发送成功'})
|
return jsonify({'message': '邮件发送成功'})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_message(f"邮件发送失败: {e}")
|
log_message(f"❌ 邮件发送失败: {e}")
|
||||||
return jsonify({'error': str(e)}), 500
|
return jsonify({'error': str(e)}), 500
|
||||||
Reference in New Issue
Block a user