为什么我发写的这个发送邮件网站无法进行发送,具体是接收到了数据但后端没有处理结果,也没有预期的成功或者报错
两组代码如下
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>信息登记表</title>
<!-- <link rel="stylesheet" href="style07.css" type="text/css"> -->
<style type="text/css">
input {
width: 200px;
height: 18px;
border: 1px solid #d4cdba;
padding: 2px;
}
.btn input {
width: 100px;
height: 30px;
border-radius: 3px;
}
</style>
</head>
<body>
<div class="bg">
<form action="#" method="get" autocomplete="off">
<h2>邮件发送</h2>
<p><span>发送者邮箱:</span>
<!-- 在下面的横线中填写`必填属性`和`自动聚焦` -->
<input type="text" name="sender" placeholder="*****@qq.com" required autofocus/>
</p>
<p><span>16位授权码:</span>
<!-- 在下面的横线中填写`必填属性`和`自动聚焦` -->
<input type="text" name="mail_pass" placeholder="16位授权码" required autofocus/>
</p>
<p><span>接收者邮箱:</span>
<!-- 在下面的横线中填写`必填属性`和`自动聚焦` -->
<input type="text" name="receivers" placeholder="*****@qq.com" required autofocus/>
</p>
<p><span>邮件标题:</span>
<!-- 在下面的横线中填写`必填属性`和`自动聚焦` -->
<input type="text" name="title" placeholder="标题" required autofocus/>
</p>
<p><span>邮件内容:</span>
<!-- 在下面的横线中填写`必填属性`和`自动聚焦` -->
<textarea style="width:200px; height:150px;" name="content" placeholder="输入想要发送的内容" required
autofocus></textarea>
<!-- <input type="text_content" style="width:200px; height:150px;" name="content" placeholder="输入想要发送的内容" required readonly/>
-->
</p>
<p class="btn">
<input type="submit" value="提交"/>
</p>
</form>
</div>
</body>
<script>
/*在这里编写submit()事件*/
function submit() {
$.ajax({
url: "submit", /*数据提交到submit处*/
type: "POST", /*采用POST方法提交*/
data: {"sender": $("#sender").val(), "mail_pass": $("#mail_pass").val(),
"receivers": $("#receivers").val(), "title": $("#title").val(),
"content": $("#content").val()},/*提交的数据(json格式),从输入框中获取*/
/*result为后端函数返回的json*/
success: function (result) {
if (result.message == "success!") {
alert(result.message)
} else {
alert(result.message)
}
}
});
}
</script>
</html>
app.py
import smtplib
from email.header import Header
from flask import Flask, render_template, request
from email.mime.text import MIMEText
def sendMail(mail_host, mail_pass, sender, title, content, receivers):
# 设置email信息
# 邮件内容设置
message = MIMEText(content, 'plain', 'utf-8')
# 邮件主题
subject = title
message['Subject'] = Header(subject, 'utf-8')
# 发送方信息
message['From'] = sender
# 接受方信息
message['To'] = receivers
# 登录并发送邮件
try:
smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 连接到服务器
smtpObj.connect(mail_host, 465)
# 登录到服务器
smtpObj.login(sender, mail_pass)
# 发送
smtpObj.sendmail(sender, receivers, message.as_string())
# 退出
smtpObj.quit()
return {'message': "success!"}
except smtplib.SMTPException as e:
return {'message': "error!"} # 打印错误
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route('/test', methods=['GET', 'POST'])
def submit():
# 由于POST、GET获取数据的方式不同,需要使用if语句进行判断
if request.method == "POST":
mail_host = "smtp.qq.com"
sender = request.form.get("sender")
mail_pass = request.form.get("mail_pass")
receivers = request.form.get("mail_pass")
title = request.form.get("title")
content = request.form.get("content")
sendMail(mail_host, mail_pass, sender, title, content, receivers)
if request.method == "GET":
mail_host = "smtp.qq.com"
sender = request.args.get("sender")
mail_pass = request.args.get("mail_pass")
receivers = request.args.get("mail_pass")
title = request.args.get("title")
content = request.args.get("content")
sendMail(mail_host, mail_pass, sender, title, content, receivers)
else:
return " 'it's not a POST operation! "
if __name__ == "__main__":
app.run(port=8080) # True表示开始debug模型
输入数据后控制台的展示