pythonflaskflask-mail

Flask Mail Type Error: missing 1 required positional argument: 'message' on Mail.send(msg)


Getting this error "TypeError: _MailMixin.send() missing 1 required positional argument: 'message'" when trying to email a forgot password message via flask-mail. Feel like I'm following the model to a tee, but I'm obviously missing something.

Here is the relevant code:

Init.py

app.config['MAIL_SERVER']='smtp.mailtrap.io'
    app.config['MAIL_PORT'] = 2525
    app.config['MAIL_USERNAME'] = [username]
    app.config['MAIL_PASSWORD'] = [password]
    app.config['MAIL_USE_TLS'] = True
    app.config['MAIL_USE_SSL'] = False
    mail = Mail(app)

models.py

class User(db.Model,UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(150), unique = True)
    password = db.Column(db.String(150))
    username = db.Column(db.String(150))
    birthdate = db.Column(db.Date)
    gender_id = db.Column(db.Integer)
    createDate = db.Column(db.DateTime(timezone = true),default = func.now())
    token = db.Column(db.String(32), nullable=False, unique=False)
    notes = db.relationship('tbl_note')

views.py The code is failing on the line mail.send(msg)

if request.method =="POST":
        user = User.query.filter_by(email = form.email.data).first()
        if user:
            #generate new token
            token = str(uuid4()).replace('-','')
            #update user token in db
            user.token = token
            db.session.add(user)
            db.session.commit()
            link = 'http://' + getenv('DOMAIN') + url_for(
                    'update_password',
                    email=my_user.email, token=token)
            msg = Message('Password Reset Request',
                  sender='noreply@demo.com',
                  recipients=[user.email])
            msg.body = 'To reset your password, visit the following link: ' + link +              
            '. If you did not make this request then simply ignore this email and no changes will be made.'
           
            Mail.send(msg)
            flash('We have sent an email to change your password. Please check your Spam folder if not found.', category = 'success')
            return redirect(url_for('auth.login'))

Solution

    1. Your object name is mail not Mail , if that doesn't work, try 2nd one.
    with mail.connect() as conn:
                msg = Message('Password Reset Request',
                      sender='noreply@demo.com',
                      recipients=[user.email])
                msg.body = 'To reset your password, visit the following link: ' + link +              
                '. If you did not make this request then simply ignore this email and no changes will be made.'
                conn.send(msg)
               
    

    & also check app.config['MAIL_USERNAME'] takes list or direct value(worked for me).