
本文详解如何在 flask 应用中接收 html 表单数据,并使用 gmail smtp 安全地发送至指定邮箱,涵盖路由处理、邮件构造、tls 配置及常见失败原因排查。
在 Flask 中实现“表单提交 → 后端接收 → 邮件发送”功能时,一个常见误区是:仅定义了 send_email() 函数,却未在对应视图函数(如 contact())中显式调用它。从你的日志可见,POST /contact 请求已成功到达服务器(状态码 200),且控制台正确打印了表单字段值(如 Sara、[email protected] 等),说明数据已成功接收;但邮件未发出,根本原因正是 send_email() 未被触发。
以下是完整的、可直接运行的解决方案:
✅ 正确的 Flask 路由与邮件调用逻辑(main.py)
from flask import Flask, render_template, request, flash
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
app = Flask(__name__)
app.secret_key = 'your-secret-key-here' # 用于 flash 消息(可选)
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
# 1. 获取表单数据
name = request.form.get('name', '').strip()
email = request.form.get('email', '').strip()
phone = request.form.get('phone', '').strip()
message = request.form.get('message', '').strip()
# 2. 基础验证(防止空提交)
if not all([name, email, message]):
flash('Please fill in all required fields.', 'error')
return render_template('contact.html', msg_sent=False)
try:
# 3. 调用邮件发送函数(关键!此前缺失这一步)
send_email(name, email, phone, message)
return render_template('contact.html', msg_sent=True)
except Exception as e:
print(f"Email sending failed: {e}")
flash('Failed to send message. Please try again later.', 'error')
return render_template('contact.html', msg_sent=False)
# GET 请求:渲染初始页面
return render_template('contact.html', msg_sent=False)
def send_email(name, email, phone, message):
# ✅ 使用 Gmail App Password(非账户密码) + 启用两步验证后生成
smtp_server = 'smtp.gmail.com'
smtp_port = 587
smtp_username = 'your-verified-gmail@gmail.com' # 替换为你的邮箱
smtp_password = 'your-16-char-app-password' # 替换为 Google 生成的 App Password
from_email = smtp_username
to_email = 'recipient@example.com' # 替换为目标邮箱(可与 from_email 相同)
subject = f'New Contact Form Submission from {name}'
# ✅ 使用标准 MIME 格式(比纯字符串更可靠,避免编码/换行问题)
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
body = f"""\
Name: {name}
Email: {email}
Phone: {phone}
Message:
{message}
"""
msg.attach(MIMEText(body, 'plain', 'utf-8'))
# ✅ 完整 SMTP 流程:连接 → TLS 加密 → 登录 → 发送
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # 启用 TLS
server.login(smtp_username, smtp_password)
server.send_message(msg) # 推荐使用 send_message() 替代 sendmail()? 关键注意事项
-
Gmail 设置必须正确:
- 开启 两步验证
- 在 App Passwords 页面 生成 16位应用专用密码(不是你的 Gmail 登录密码!)
- 确保 smtp_username 是你启用两步验证的 Gmail 账户(如 example@gmail.com)
HTML 表单需匹配后端字段名:
你的 contact.html 中 、不要忽略异常处理:
如示例中加入 try...except,能快速定位 SMTP 连接失败、认证错误或网络问题(例如防火墙拦截端口 587)。-
安全提醒:
切勿将 smtp_password 硬编码在代码中!生产环境应使用环境变量:import os smtp_password = os.getenv('GMAIL_APP_PASSWORD')并通过 .env 文件或系统环境变量管理。
✅ 验证是否成功
- 成功时:页面显示 “Successfully sent your message”,且收件箱收到格式清晰的邮件;
- 失败时:控制台打印详细错误(如 smtplib.SMTPAuthenticationError),据此调整凭据或网络配置。
遵循以上结构,你的 Flask 表单邮件功能即可稳定运行——核心在于 确保 send_email() 在 POST 请求处理逻辑中被主动调用,而非仅“存在”于文件中。










