0

i am working on this email confirmation app using Flask. for this i am using flask_mail(obviously) but i am getting this error:

TypeError: argument of type 'Mail' is not iterable

@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)

Here is the template code

<!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>
vatsalay
  • 469
  • 1
  • 9
  • 21

1 Answers1

0

You passed the wrong value into recipients.

Here:

@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)

You passed into the recipients parameter, a list with the mail variable in it instead of a list of email addresses you intend sending the mails to. I assume you are passing in the Mail object into the recipient.

I assume the address you want to send the email to, is in the getting variable.

Solution:

@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)
HAKS
  • 419
  • 4
  • 9
  • the error is gone but now i am getting this error : **TypeError: 'NoneType' object is not iterable** – vatsalay Aug 31 '19 at 08:51
  • print the value for **getting**. Note that the email address as a string should be in that list passed into recipients. – HAKS Aug 31 '19 at 09:01
  • For example:... recipients = ["test@gmail.com"] where test@gmail.com is just an example. – HAKS Aug 31 '19 at 09:02
  • @vatsalay I have made edits to the code, so you can send the message to your own address as a test. – HAKS Aug 31 '19 at 09:14
  • actually i tried it before and that worked for me but i want to grab that recipient value from my template(form) but i am getting this error: **TypeError: 'NoneType' object is not iterable** – vatsalay Aug 31 '19 at 11:43
  • Add the template code then. The info provided is not sufficient. – HAKS Aug 31 '19 at 11:48
  • I'd advise you use Flask Forms for your forms. – HAKS Sep 03 '19 at 17:14