TypeError:“邮件”类型的参数不可迭代

时间:2019-08-31 02:17:28

标签: python-3.x flask flask-mail itsdangerous

我正在使用Flask开发此电子邮件确认应用程序。为此,我正在使用flask_mail(显然),但出现此错误:

TypeError:“邮件”类型的参数不可迭代

@app.route('/email', methods=['GET', 'POST'])
def send_mail():
    getting = request.form.get('mail')
    token = s.dumps(getting, salt='email-confirm')

    msg = Message('Confirm Email', sender='vatsalayvk1434@gmail.com', recipients=[mail])

    link = url_for('confirm_mail', token=token, _externel=True)
    msg.body = f'Your Link is {link}'

    mail.send(msg)
    return render_template('confirm.html', getting=getting, token=token)

这是模板代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="{{ url_for('static',filename='dist/css/bootstrap.min.css') }}">
    <title>Document</title>
</head>
<body>
    <div class="col-lg-12">
        <h2 class="display-4 mb-3">Flask Confirm Message App </h2>
        <div class="col-md-6">
                <form action="/email" method="post" class="mt-3 align-items-center justify-content-center">
                    <input type="text" name="mail" class="form-control form-control-sm">
                    <input type="submit" class="btn btn-outline-primary mt-4 ml-3">
                </form>
            </div>
            <h2>The Mail is  : {{getting}}</h2>
            <h2>The token is : {{token}}</h2>
    </div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您将错误的值传递给了收件人

这里:

@app.route('/email', methods=['GET', 'POST'])
def send_mail():
    getting = request.form.get('mail')
    token = s.dumps(getting, salt='email-confirm')

    msg = Message('Confirm Email', sender='vatsalayvk1434@gmail.com', recipients=[mail])

    link = url_for('confirm_mail', token=token, _externel=True)
    msg.body = f'Your Link is {link}'

    mail.send(msg)
    return render_template('confirm.html', getting=getting, token=token)

您传递到了收件人参数中,该参数中包含一个带有 mail 变量的列表,而不是要将邮件发送到的电子邮件地址的列表。我假设您正在将Mail对象传递给收件人。

我假设您要发送电子邮件的地址位于获取变量中。

解决方案:

@app.route('/email', methods=['GET', 'POST'])
def send_mail():
    getting = request.form.get('mail')
    token = s.dumps(getting, salt='email-confirm')

    # msg = Message('Confirm Email', sender='vatsalayvk1434@gmail.com', recipients=[getting])
    # To send the mail to yourself
    msg = Message('Confirm Email', sender='vatsalayvk1434@gmail.com', recipients=['vatsalayvk1434@gmail.com'])

    link = url_for('confirm_mail', token=token, _externel=True)
    msg.body = f'Your Link is {link}'

    mail.send(msg)
    return render_template('confirm.html', getting=getting, token=token)