0

我正在使用 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>
4

1 回答 1

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 对象传递给收件人。

我假设您要将电子邮件发送到的地址在getting变量中。

解决方案:

@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)
于 2019-08-31T04:37:58.897 回答