LoginSignup
0
0

More than 3 years have passed since last update.

windows上でmaildir形式のthunderbirdのメールをsylpheedに移す

Posted at

結論

  • pythonでファイルを一つずつ開いて文字コードと改行コードだけ変換して保存するのが簡単で早い

thunderbirdのメール保存形式

sylpheedのメール保存形式

  • mh形式
  • よく見てみると改行コードが\nだったり,文字コードが合わなかったりで,maildirをそのまんまコピーしたのでは読み込めない
  • 構造はクソ単純なので,maildirを文字コードと改行コードだけ変換して連番でつっこめば動作しそうではある

コード

  • このプログラムを実行したカレントディレクトリにimportHogehogeという感じの名前でMH形式でメールが保存される.
  • i = 1を適当な数字に変えることで,既に利用しているMH形式のメールの続きとして出力できる
import mailbox
import os

path_to_maildir = 'hogehoge'  # thunderbirdの受信箱 'Inbox' フォルダの入っているフォルダ
targets = ['Inbox', 'Inbox/subdir', ]

for target in targets:
    mhdir = f"import{target.replace('/', '')}"
    mdir  = f"{path_to_maildir}/{target.replace('/','.sbd/')}"
    _ = mailbox.MH(mhdir, create=True)

    files = os.listdir(f'{mdir}/cur')
    i = 1
    for f in files:
        if f[0] == '.':
            continue
        with open(f'{mdir}/cur/{f}', encoding='utf-8') as fobj:
            with open(f'{mhdir}/{i}', 'wb') as wobj:
                try:
                    buf = fobj.read()
                except Exception as ex:
                    # エンコーディングが違っている場合
                    # 私の環境ではJISはなかったようのでスルーした
                    # 必要に応じてtryの階層を掘っていけば何とかなる
                    with open(f'{mdir}/cur/{f}', encoding='cp932') as fobj2:
                        buf = fobj2.read()
                wobj.write(buf.replace('\r','').encode('utf-8'))
            i += 1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0