开发者

Can I parse an email saved as a text file and retrieve/save its attachment using Perl?

I'm using Perl & MAIL::IMAPClient to save emails from Gmail as .txt files using the IMAPClient method:

message_to_file

These files seem to contain the attachments of emails encoded as text.

Here's some of the text file:

--0015174c1274ee7ca60495ca69d5
Content-Type: video/3gpp; name="20101112233055.3gp"
Content-Disposition: attachment; filename="20101112233055.3gp"
Content-Transfer-Encoding: base64
X-Attachme开发者_StackOverflownt-Id: 1353288501407252480-1

AAAAHGZ0eXAzZ3A0AAADADNncDRtcDQxM2dwNgAFHyltZGF0AAABthAwrMK9/Mue7fM+95wsf9P8
WI7mPzzp/ikijbucv72j7OywVGuh5kBzo89Zra6PihxZg0zadDqihZFpsPJeG36Ihk9qZW+LLQ2u
NEd96vsqgpnLFnhhwGBWgL2Xpt0cXkW....[A LOT MORE CHARS]....AAAQAAAALAAAAAQAAABRzdHN6
AAAAAAAAACAAAAChAAAAIHN0Y28AAAAAAAAABAABHNoAASMaAALYFwAFHeU=
--0015174c1274ee7ca60495ca69d5--

I can't find any method that will save the attachment separately. Is there any way to do this via parsing?


What you've got there is the raw text of a MIME-encoded email message. Most languages have a general purpose MIME library for parsing these. A quick search on CPAN reveals that MIME::Parser might do the trick:

use MIME::Parser;

open(FH, '/var/tmp/test.txt');
my $parser = new MIME::Parser;
my $entity = $parser->parse(\*FH) or die;

$entity->dump_skeleton;

This will dump the various parts of the email stored in the plain text file test.txt to /var/tmp. Check out the MIME::Parser docs for further options.


This is a suggestion for a workaround. You would engage this logic after you were past the headers.

use MIME::Base64;

my $attachlines = '';
while ( <$input> ) { 
    last if index( $_, '--0015174c1274ee7ca60495ca69d5--' ) == 0;
    $attachlines .= $_;
}

my $attach = MIME::Base64::decode( $attachlines );

Note: I take for granted that you know how MIME multi-part forms work, so I don't discuss how to programmatically get the divider '--0015174c1274ee7ca60495ca69d5--'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜