Flask Mail Error:AttributeError:' NoneType'对象没有属性' split'

时间:2016-12-15 09:24:46

标签: python email flask wtforms flask-mail

以下是错误的追溯:

Traceback (most recent call last):
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\PYTHON27\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\richard.danvers\Application\app\views.py", line 62, in index
    department_data=form.department.data
  File "C:\Users\richard.danvers\Application\app\views.py", line 30, in send_email
    mail.send(msg)
  File "C:\PYTHON27\lib\site-packages\flask_mail.py", line 492, in send
    message.send(connection)
  File "C:\PYTHON27\lib\site-packages\flask_mail.py", line 427, in send
    connection.send(self)
  File "C:\PYTHON27\lib\site-packages\flask_mail.py", line 190, in send
    message.as_bytes() if PY3 else message.as_string(),
  File "C:\PYTHON27\lib\site-packages\flask_mail.py", line 381, in as_string
    return self._message().as_string()
  File "C:\PYTHON27\lib\site-packages\flask_mail.py", line 349, in _message
    f = MIMEBase(*attachment.content_type.split('/'))
AttributeError: 'NoneType' object has no attribute 'split'

这个问题是由我的电子邮件功能引起的,该功能应该附加一个已经上传到我的wtform的文件。我相信我可能错误地引用了附件。这是功能:

def send_email(subject, sender, recipients, text_body):
    FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd']) # list of accepted file types for file uploads
    form = ApplicationForm (request.files) #request submitted files from the Application Form
    submit_name = form.file_upload.data.filename #declares a variable for the submitted file name
    mail = Mail(app)
    msg = Message(subject, sender=sender, recipients=recipients) 
    msg.body = text_body
    if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES: #if file is in list of approved file types
        filename = secure_filename(submit_name) #hashes the filename for added security
        form.file_upload.data.save('uploads/' + filename) #saves file to uploads folder
        with app.open_resource('uploads/' + filename) as fp:
            msg.attach(filename, data=fp.read()) #attaches the submitted file to the email
            print 'file sent successfully'
    mail.send(msg) 

有人能看到这个问题吗?

1 个答案:

答案 0 :(得分:0)

我没有声明内容类型并且引用错误。

已更改:

msg.attach(filename, data=fp.read())

要:

msg.attach(filename, 'text/plain', fp.read())