Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask mail error: headers = [self.sender, self.reply_to] + self.recipients

I'm supposed to explain why my question is not a duplicate of: TypeError: can only concatenate list (not “str”) to list

...so it's not a duplicate because that post deals with the concatenation operator, +, appearing in the code; whereas my issue has that operator in the error message but not in the code.

Here is the relevant Flask code, I looked through the traceback (shown below)... and it seems like the

mail.sent(msg)

line is where the offense happens. I have tried really hard to fix this, apparently something is wrong with the contents of msg... but I can't figure out what. Any help would be much appreciated!

            token = s.dumps(form.email.data, salt='email-confirm')
            subject = 'subject goes here'
            msg = Message(subject=subject, sender='[email protected]',
                          recipients=form.email.data)
            link = url_for('confirm_email', token=token, _external=True)
            pull_row = User.query.filter_by(email=form.email.data).first()
            firstname = pull_row.firstname
            msg.html = render_template("email_confirmationemail.html", link=link, name=firstname)
            mail.send(msg)
            return redirect(url_for('checkyouremail'))

Here is the traceback

2017-08-07T06:00:05.982678+00:00 app[web.1]: Traceback (most recent call last):
2017-08-07T06:00:05.982679+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 130, in handle
2017-08-07T06:00:05.982680+00:00 app[web.1]:     self.handle_request(listener, req, client, addr)
2017-08-07T06:00:05.982681+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 171, in handle_request
2017-08-07T06:00:05.982681+00:00 app[web.1]:     respiter = self.wsgi(environ, resp.start_response)
2017-08-07T06:00:05.982682+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1836, in __call__
2017-08-07T06:00:05.982682+00:00 app[web.1]:     return self.wsgi_app(environ, start_response)
2017-08-07T06:00:05.982683+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1820, in wsgi_app
2017-08-07T06:00:05.982684+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1403, in handle_exception
2017-08-07T06:00:05.982684+00:00 app[web.1]:     response = self.make_response(self.handle_exception(e))
2017-08-07T06:00:05.982685+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2017-08-07T06:00:05.982686+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
2017-08-07T06:00:05.982686+00:00 app[web.1]:     raise value
2017-08-07T06:00:05.982687+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1817, in wsgi_app
2017-08-07T06:00:05.982688+00:00 app[web.1]:     response = self.full_dispatch_request()
2017-08-07T06:00:05.982689+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2017-08-07T06:00:05.982688+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1477, in full_dispatch_request
2017-08-07T06:00:05.982690+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1381, in handle_user_exception
2017-08-07T06:00:05.982691+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2017-08-07T06:00:05.982691+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
2017-08-07T06:00:05.982692+00:00 app[web.1]:     raise value
2017-08-07T06:00:05.982692+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1475, in full_dispatch_request
2017-08-07T06:00:05.982693+00:00 app[web.1]:     rv = self.dispatch_request()
2017-08-07T06:00:05.982694+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1461, in dispatch_request
2017-08-07T06:00:05.982695+00:00 app[web.1]:   File "/app/app.py", line 213, in login
2017-08-07T06:00:05.982695+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2017-08-07T06:00:05.982696+00:00 app[web.1]:     mail.send(msg)
2017-08-07T06:00:05.982697+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_mail.py", line 492, in send
2017-08-07T06:00:05.982697+00:00 app[web.1]:     message.send(connection)
2017-08-07T06:00:05.982698+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_mail.py", line 427, in send
2017-08-07T06:00:05.982698+00:00 app[web.1]:     connection.send(self)
2017-08-07T06:00:05.982699+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_mail.py", line 181, in send
2017-08-07T06:00:05.982700+00:00 app[web.1]:     if message.has_bad_headers():
2017-08-07T06:00:05.982700+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_mail.py", line 400, in has_bad_headers
2017-08-07T06:00:05.982701+00:00 app[web.1]:     headers = [self.sender, self.reply_to] + self.recipients
2017-08-07T06:00:05.982704+00:00 app[web.1]: TypeError: can only concatenate list (not "str") to list
like image 795
okcapp Avatar asked Jan 03 '23 15:01

okcapp


2 Answers

So here is what solved the problem I was having. (This is about the recipients parameter in Message). I have only one recipient. But I suppose many recipients are expected. In any event, changing my one recipient from a string to a list did the trick. For example going from

recipients=email

to

recipients=email.split()

fixes the problem. I'm new to flask-mail and I didn't think twice about using a string for recipients.

like image 181
okcapp Avatar answered Jan 13 '23 12:01

okcapp


I ran into the same problem today here is how I solved it.

recipient = request.form.get("email")
Message(subject=subject, recipients=[recipient]

recipients should be passed as a list even if you have one.

Parameters:

  • recipients – list of email addresses

API

like image 32
iJustin254 Avatar answered Jan 13 '23 13:01

iJustin254