TypeError:' str'使用flask sendmail时不支持缓冲区接口

时间:2015-10-02 14:03:43

标签: python python-3.x flask sendmail

我试图在烧瓶内使用sendmail发送一些电子邮件 - 但回来的错误信息有点神秘。

from flask.ext.sendmail import Message
from flask.ext.sendmail import Mail

mail = Mail()
mail.init_app(app)
msg = Message("Hello".encode('utf-8'), sender="xxx@xxx.com".encode('utf-8'), recipients="xxx@xxx.com".encode('utf-8'))
msg.body = "testing"
msg.html = "testing"
mail.send(msg)

问题出现在mail.send(msg)。以下是完整的跟踪:

Traceback (most recent call last):
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/xxx/xxx/login.py", line 129, in registersub
    mail.send(msg)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_sendmail/mail.py", line 40, in send
    message.send(self.connect())
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_sendmail/message.py", line 115, in send
    if self.is_bad_headers():
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_sendmail/message.py", line 72, in is_bad_headers
    if c in val:
TypeError: 'str' does not support the buffer interface

可能的罪魁祸首在哪里?

1 个答案:

答案 0 :(得分:0)

看一下这个例子

>>> 'c' in b'val'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface

你得到的是同样的错误,所以不要对你正在使用的str值进行编码。