Search code examples
pythonflaskflask-mail

Flask-Mail how to add multiple files to message?


I want to add several files like attachment, but I can not understand how.

My code now looking like.

@form.post('/')
def get_data_from_form():
    message = request.form['message']
    grecaptcha = request.form['g-recaptcha-response']
    remote_ip = request.remote_addr
    files = request.files.getlist('file')
    msg = Message('EMAIL FROM FORM', recipients=['admin@****'])
    if check_recaptcha(grecaptcha, remote_ip):
        for file in files:
            mimetype = file.content_type
            filename = secure_filename(file.filename)
            msg.attachments = file
            msg.attach(filename, mimetype)
        msg.body = message
        try:
            mail.send(msg)
            return {'msg': 'The message has sent'}
        except Exception as err:
            logger.debug(err)
            return {'msg': False}


Solution

  • In order to solve the problem, I just had to add msg.attachments