TypeError: can only concatenate list (not "str") to list -

TypeError: can only concatenate list (not "str") to list -

我正在尝试在用户提交联系我们表单时收到一封电子邮件。所有数据都在数据库中成功更新,但在将此详细信息发送到我的邮件时出现错误:-TypeError:只能将列表(不是“str”)连接到列表。我没有添加任何“+”号,我在其中添加任何内容。

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail, Message
import json

with open('./templates/config.json','r') as f:
    params = json.load(f)["params"]

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp.gmail.com',
    MAIL_PORT=465,
    # MAIL_PORT=587,
    MAIL_USE_SSL=True,
    MAIL_USERNAME=params['gm_user'],
    MAIL_PASSWORD=params['gm_password']

)
mail = Mail(app)


if params['local_server']:
    app.config['SQLALCHEMY_DATABASE_URI'] = params['local_uri']
else:
    app.config['SQLALCHEMY_DATABASE_URI'] = params['prod_uri']

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Contacts(db.Model):
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), primary_key=False)
    phone = db.Column(db.String(13), unique=False, nullable=False,primary_key=False)
    email = db.Column(db.String(120), unique=False, nullable=False,primary_key=False)
    msg = db.Column(db.String(500), nullable=False,primary_key=False)



@app.route("/")
def home():
    return render_template('index.html',urlDict = params)

@app.route("/post")
def post():
    return render_template('post.html',urlDict = params)

@app.route("/contact",methods = ['GET','POST'])
def contact():
    if request.method == 'GET':
        return render_template('contact.html',urlDict = params)
    elif request.method =='POST':
        fName = request.form.get("name")
        fEmail = request.form.get("email")
        fMsg = request.form.get("msg")
        fPhone = request.form.get("phone")
        entry = Contacts(name=fName, email=fEmail, msg=fMsg, phone=fPhone)
        db.session.add(entry)
        db.session.commit()
        **mail.send_message(subject='', sender=fEmail, recipients=params['gm_user'], body = '')**

        return render_template('contact.html',urlDict = params)

@app.route("/about")
def about():
    return render_template('about.html',urlDict = params`enter code here`)

app.run(debug=True)

您必须在调用 send_message 时将列表传递给 recipients。现在你正在传递一个字符串(即 params['gm_user']),flask_mail 试图在内部某处连接到一个列表。

查看文档中的第一个示例:https://pythonhosted.org/Flask-Mail/#sending-messages

recipients=params['gm_user'] 中,出现错误是因为收件人需要一个列表

params['gm_user']

但在这里,它是以字符串形式读取的。只需将其更改为以下内容:

recipients=[params['gm_user']]

这会将列表传递给收件人并且它会起作用!