end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

Mail::MboxParser でMbox file(例:thunderbird)から添付fileをエクスポート

http://search.cpan.org/perldoc?Mail::MboxParser

thunderbirdのメールにある添付ファイルをエクスポートする必要が出てきて、cpanを調べてみたら、Mail::MboxParser というモジュールがありました。

thunderbirdで受信したメールは、mbox形式で c:/Documents and Settings/ 以下の深い場所にあるprofileにありますが、例えば、次のようなscriptで添付ファイルを抽出できます。

#!/usr/local/bin/perl
use strict;
use warnings;
use Mail::MboxParser;

main();

sub main {
    my $parseropts = {
                      enable_cache    => 1,
                      enable_grep     => 1,
                      cache_file_name => 'mail/cache-file',
                     };
                                  # mbox fileを指定
    my $mb = Mail::MboxParser->new('/home/endo/tmp/mail',
                                   decode     => 'ALL',
                                   parseropts => $parseropts);

    for my $msg ($mb->get_messages) {
        print $msg->header->{subject}, "\n";
                                    #添付ファイルをexport
        $msg->store_all_attachments(path => '/home/endo/tmp/at_data/');
    }
}