烧瓶邮件错误:标题= [self.sender,self.reply_to] + self.recipients
Posted
技术标签:
【中文标题】烧瓶邮件错误:标题= [self.sender,self.reply_to] + self.recipients【英文标题】:flask mail error: headers = [self.sender, self.reply_to] + self.recipients 【发布时间】:2017-08-07 06:10:05 【问题描述】:我应该解释为什么我的问题不是重复的:TypeError: can only concatenate list (not "str") to list
...所以它不是重复的,因为该帖子处理代码中出现的连接运算符 +;而我的问题是错误消息中有该运算符,但代码中没有。
这是相关的 Flask 代码,我查看了回溯(如下所示)...看起来像
mail.sent(msg)
line 是攻击发生的地方。我已经非常努力地解决这个问题,显然味精的内容有问题......但我不知道是什么。任何帮助将不胜感激!
token = s.dumps(form.email.data, salt='email-confirm')
subject = 'subject goes here'
msg = Message(subject=subject, sender='recipient@email.com',
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'))
这是回溯
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
【问题讨论】:
TypeError: can only concatenate list (not "str") to list的可能重复 【参考方案1】:所以这就是解决我遇到的问题的方法。 (这是关于 Message 中的收件人参数)。我只有一个收件人。但我想会有很多收件人。无论如何,将我的一个收件人从一个字符串更改为一个列表就可以了。 例如从
recipients=email
到
recipients=email.split()
解决了这个问题。我是烧瓶邮件的新手,我没有三思而后行地为收件人使用字符串。
【讨论】:
你是对的。收件人必须始终是一个列表。 我使用recipients=[form.email.data]
作为正在输入表单的电子邮件。
我根据你的建议实现了这个if type(recipients) is list: msg.recipients = recipients else: msg.recipients = recipients.split()
谢谢@Chockomonkey,收件人必须是一个列表:)【参考方案2】:
我今天遇到了同样的问题,这是我如何解决的。
recipient = request.form.get("email")
Message(subject=subject, recipients=[recipient]
即使您有收件人,也应将收件人作为列表传递。
参数:
收件人 - 电子邮件地址列表
API
【讨论】:
以上是关于烧瓶邮件错误:标题= [self.sender,self.reply_to] + self.recipients的主要内容,如果未能解决你的问题,请参考以下文章