MailArchivePluginとの死闘(一章)
問題は一つずつ、より致命傷のものから。まずは複数プレーンテキスト領域のあるメールで最後のパート以外ロストしてしまう件。やばそうなところは比較的容易に絞り込めた。
for part in msg.walk(): content_type = part.get_content_type() self.print_debug('Content-Type:'+content_type) file_counter = 1 if content_type == 'multipart/mixed': pass elif content_type == 'text/html' and self.is_file(part) == False: body = part.get_payload(decode=1) elif content_type == 'text/plain' and self.is_file(part) == False: body = part.get_payload(decode=1) charset = part.get_content_charset() self.print_debug('charset:'+str(charset)) # Todo:need try if charset != None: body = self.to_unicode(body,charset)
多分一本のメールの中にtextタイプのパートが複数在るケースを想定してなくてbody変数が上書きされてるっぽい。
個々のテキスト領域の文字コードが違っていたりする場合も考慮してワンクッション間に挟んで結合処理すればよさげ。
最終的にbody変数はSQLのdbにinsertされてるようなのでその辺はそのままにしておく。