下载电子邮件附件

Download email attachment

我正在使用 IMAP 服务器阅读电子邮件 Mail::IMAPClient 通过 SSL 的模块。 我能够提取电子邮件的正文但不能提取附件。

请建议我如何下载电子邮件附件?我想避免在附件中下载附件。即多级附件。

这是我获取电子邮件正文的方式

my $struct = $client->get_bodystructure( $msg_id );

if ( $struct->bodytype ne "MULTIPART" ) {
    print "\n BodyEnc:" . $struct->bodyenc();
}

my $rDecode = decode( $struct, $client, $msg_id );

if ( $rDecode ne "" && ( length( $rDecode ) > 2 ) ) {
    print( $rDecode . "\n" );
}

foreach my $dumpme ( $struct->bodystructure() ) {

    if ( $dumpme->bodytype() eq "MULTIPART" ) {
        next;
    }

    $rDecode = "";

    $rDecode = decode( $dumpme, $client, $msg_id );

    if ( ( $rDecode ne "" ) && ( length( $rDecode ) > 2 ) ) {
        print( $rDecode . "\n" );
    }
}

sub decode {

    my ( $process, $imap, $msg_id ) = @_;

    if ( $process->bodytype eq "TEXT" ) {

        if ( $process->bodyenc eq "base64" ) {

            return decode_base64( $imap->bodypart_string( $msg_id, $process->id ) );
        }
        elsif ( index( " -7bit- -8bit- -quoted-printable- ", lc( $process->bodyenc ) ) != -1 ) {

            return $imap->bodypart_string( $msg_id, $process->id );
        }
    }

我终于可以下载电子邮件附件了。我只需要创建一个与电子邮件中的附件具有相同扩展名的新文件,并通过解码附件内容将内容写入其中。