Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fail when attachment filename too long #138

Open
FeihuaLiu opened this issue Dec 16, 2016 · 0 comments
Open

fail when attachment filename too long #138

FeihuaLiu opened this issue Dec 16, 2016 · 0 comments

Comments

@FeihuaLiu
Copy link

def send_mail(self):
    title = '【数据报表】' + self._title
    print(self._title)
    msg = Message(
        title,
        sender=('数据报表', '[email protected]'),
        recipients=list(current_app.config[
                            'CUSTOMIZED_QRCODE_SUBSCRIBE_DAILY_REPORT_RECIPIENTS']))

    xlsx = self.make_xlsx()
    msg.body = '''开始时间: %s\n结束时间: %s\n''' \
               '''数据见附件,如有问题请联系[email protected]''' % (
                   self.start_at.strftime('%Y-%m-%d %H:%M:%S'),
                   self.end_at.strftime('%Y-%m-%d %H:%M:%S'),
               )

    **# file_name='的打算打算大安大厦大大大'  fail
    # file_name = '的打算打算大安大厦大' pass
    # file_name = '的打算打算大安大厦大大' pass
    # file_name ='深圳测试中升雷克萨斯 - 二维码关注 - 20161216' fail**
    file_name = '二维码关注-%s' % (self.start_at.strftime('%Y%m%d'),)

    msg.attach(
        self._title + '.xlsx',  # 这个好像并没有什么用,什么字符串对收到的邮件都无影响
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        xlsx.getvalue(),
        # 'attachment; filename="%s.xlsx"' % self._title
        'attachment; filename="%s.xlsx"' % file_name
        # 这个filename不能太长,否则会报错,发不出去
    )

    mail.send(msg)

I use Flask==0.10.1 and Flask-Mail==0.9.1 with python 3.5.1, to send email as above code . I found it will fail when attachment filename is long, as I comment fail. error log is below, I am not sure is bug of flask mail or python3.5.1, after google can't find how to fix this.

Traceback (most recent call last):
File "manage.py", line 31, in
manager.run()
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_script/init.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_script/init.py", line 383, in handle
res = handle(*args, **config)
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_script/commands.py", line 216, in call
return self.run(*args, **kwargs)
File "/Users/brad/work/ZS-wecaht-web/app/commands/dev.py", line 281, in send_customized_qrcode_subscribe_daily_report
customized_qrcode_subscribe_daily_report(store_id)
File "/Users/brad/work/ZS-wecaht-web/app/reports.py", line 24, in customized_qrcode_subscribe_daily_report
report.send_mail()
File "/Users/brad/work/ZS-wecaht-web/app/reports.py", line 127, in send_mail
mail.send(msg)
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_mail.py", line 492, in send
message.send(connection)
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_mail.py", line 427, in send
connection.send(self)
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_mail.py", line 190, in send
message.as_bytes() if PY3 else message.as_string(),
File "/Users/brad/work/ZS-wecaht-web/venv/lib/python3.5/site-packages/flask_mail.py", line 385, in as_bytes
return self._message().as_bytes()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/message.py", line 179, in as_bytes
g.flatten(self, unixfrom=unixfrom)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 115, in flatten
self._write(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 181, in _write
self._dispatch(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 214, in _dispatch
meth(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 272, in _handle_multipart
g.flatten(part, unixfrom=False, linesep=self._NL)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 115, in flatten
self._write(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 195, in _write
self._write_headers(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/generator.py", line 422, in _write_headers
self._fp.write(self.policy.fold_binary(h, v))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/policy.py", line 192, in fold_binary
return folded.encode(charset, 'surrogateescape')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 42-51: ordinal not in range(128)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant