小结:
Topic source
if content_type == 'text/plain' or content_type == 'text/html':
...
else:
print('%sAttachment: %s' % (' ' * indent, content_type))
老师的这句是判断是否是正文,否则为附件,如果附件是txt文本就可能是误判了,能否直接改成:
if 'attachment' not in msg.get('Content-Disposition',''):
...
else:
print('%sAttachment: %s' % (' ' * indent, content_type))
- 1
林er爱喝果汁Q
出现问题: 我的邮件附件里的txt文本,不会被当做attachment,而会进入
分析: text文件里没有headers,当然guess不出来。所以问题是它怎么会被认为不是附件呢, 原因是包含这个text文件的MIME使用get_content_type()方法时返回了'text/plain',但get_charset()却返回了空。 包含text附件的MIME的headers如下:
对比表示正文的MIME的headers如下:
解决方法:
老师这句的逻辑目的是判断是否是正文,在它之前先判断附件用查找header 'Content-Disposition'的值是否含有'attachment'关键词,如下:
测试通过。