Flask-Mail Recursion将文件附加到电子邮件时出错

时间:2017-02-24 18:58:49

标签: python flask flask-mail

当我尝试使用Flask-Mail将文件附加到电子邮件时,我得到RecursionError。不附加文件时代码工作正常。我该如何解决这个问题?

import mimetypes

def send_message(to, subject, message, attachment_path=None):
    msg = Message()
    msg.sender = ('{} {}'.format(
        current_user.first_name.capitalize(),
        current_user.last_name.capitalize()
    ), current_user.email)
    msg.subject = subject
    msg.recipients.append(to)
    msg.html = message.replace("\n", "<br/>")

    if attachment_path:
        with app.open_resource(attachment_path, mode='rb') as my_file:
            extension = attachment_path[attachment_path.index('.'):]
            content_type = mimetypes.types_map[extension]
            data = my_file.read()

        msg.attach(filename=attachment_path, content_type=content_type, data=data)

    mail.send(msg)
Traceback (most recent call last):
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Program Files\Anaconda3\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask_login\utils.py", line 228, in decorated_view
    return func(*args, **kwargs)
  File "C:\Users\Ziyad Yehia\OneDrive\flask practice\duty_report_sender_2\app\users\views.py", line 231, in message
    send_message(recipient.email, subject=form.subject.data, message=form.message.data, attachment_path = attachment_path)
  File "C:\Users\Ziyad Yehia\OneDrive\flask practice\duty_report_sender_2\app\email.py", line 85, in send_message
    mail.send(msg)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask_mail.py", line 492, in send
    message.send(connection)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask_mail.py", line 427, in send
    connection.send(self)
  File "C:\Program Files\Anaconda3\lib\site-packages\flask_mail.py", line 190, in send
    message.as_bytes() if PY3 else message.as_string(),
  File "C:\Program Files\Anaconda3\lib\site-packages\flask_mail.py", line 385, in as_bytes
    return self._message().as_bytes()
  File "C:\Program Files\Anaconda3\lib\email\message.py", line 179, in as_bytes
    fp = BytesIO()
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 115, in flatten
    self.write(ufrom + self._NL)
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 181, in _write
    self._fp = sfp = self._new_buffer()
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 214, in _dispatch
    meth = self._writeBody
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 272, in _handle_multipart
    g = self.clone(s)
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 115, in flatten
    self.write(ufrom + self._NL)
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 195, in _write
    if meth is None:
  File "C:\Program Files\Anaconda3\lib\email\generator.py", line 422, in _write_headers
    # strings with 8bit bytes.
  File "C:\Program Files\Anaconda3\lib\email\policy.py", line 190, in fold_binary
    folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
  File "C:\Program Files\Anaconda3\lib\email\policy.py", line 204, in _fold
    return self.header_factory(name, ''.join(lines)).fold(policy=self)
  File "C:\Program Files\Anaconda3\lib\email\headerregistry.py", line 255, in fold
    return header.fold(policy=policy)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 300, in fold
    self._fold(folded)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 1228, in _fold
    rest._fold(folded)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 347, in _fold
    if folded.append_if_fits(part):
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 149, in append_if_fits
    token._fold(self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 338, in _fold
    if folded.append_if_fits(part, tstr):
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 149, in append_if_fits
    token._fold(self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 338, in _fold
    if folded.append_if_fits(part, tstr):
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 149, in append_if_fits
    token._fold(self)
# ~1000 recursions removed
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 325, in _fold
    tstr = str(part)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 196, in __str__
    return ''.join(str(x) for x in self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 196, in <genexpr>
    return ''.join(str(x) for x in self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 196, in __str__
    return ''.join(str(x) for x in self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 196, in <genexpr>
    return ''.join(str(x) for x in self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 196, in __str__
    return ''.join(str(x) for x in self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 196, in <genexpr>
    return ''.join(str(x) for x in self)
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 633, in __str__
    return quote_string(''.join(str(x) for x in self))
  File "C:\Program Files\Anaconda3\lib\email\_header_value_parser.py", line 633, in <genexpr>
    return quote_string(''.join(str(x) for x in self))
RecursionError: maximum recursion depth exceeded while calling a Python object

0 个答案:

没有答案