pgadekar has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using MIME::Parse to extract the content of the mail.
Code that I am using:

#!/usr/bin/perl -w # Always be safe use strict; use warnings; # Use the module use Mail::IMAPClient; use MIME::Parser; use Data::Dumper; use constant { MULTIPART => "MULTIPART", TEXT => "TEXT" }; my $imap = Mail::IMAPClient->new( Server => "localhost", User => 'pgadekar', password => "pgadekar", Port => 143, Ssl=> 0, Uid=> 1) or die "IMAP Failure: $@"; $imap->select("INBOX") or die "IMAP Select Error: $@"; foreach my $box qw( INBOX ) { # How many msgs are we going to process print "There are ". $imap->message_count($box). " messages in the + $box folder.\n"; # Select the mailbox to get messages from $imap->select($box) or die "IMAP Select Error: $@"; # Store each message as an array element my @msgseqnos = $imap->messages() or die "Couldn't get all message +s $@\n"; # Loop over the messages and store in file foreach my $seqno (@msgseqnos) { my $parser = MIME::Parser->new; my $entity = $parser->parse_data($imap->message_string($seqno) +); my $header = $entity->head; my $from = $header->get_all("From"); my $msg_id = $header->get("message-id"); my $to = $header->get_all("To"); my $date = $header->get("date"); my $subject = $header->get("subject"); print "From: ". Dumper($from); print "Message-id: $msg_id"; print "To: $to"; print "Date: $date"; print "Subject: $subject"; my $content = get_msg_content($entity); $entity->purge(); print "Content: $content"; } # Expunge and close the folder $imap->expunge($box); $imap->close($box); } # We're all done with IMAP here $imap->logout(); sub split_entity { local $entity = shift; my $num_parts = $entity->parts; # how many mime parts? if ($num_parts) { # we have a multipart mime message foreach (1..$num_parts) { split_entity( $entity->parts($_ - 1) ); # recursive call } } else { # we have a single mime message/part if ($entity->effective_type =~ /^text\/plain$/) { # text messa +ge print "Part Content: " . $entity->bodyhandle->as_string; } else { # no text message print "Attachment Content: ". handle_other($entity->bodyha +ndle->path); } } } sub handle_other() { local $path = shift; }

What should I write in sub handle_other so that I can get the content of the attachment ?

Replies are listed 'Best First'.
Re: Extracting Attachments Using MIME::Parse
by samarzone (Pilgrim) on Dec 10, 2010 at 08:22 UTC

    If $path is the physical path of the file you can use open and related functions to read the file.

    Also note that you have strictly told compiler not to accept any arguments in subroutine handle_other by giving blank parens. Your call handle_other($entity->bodyhandle->path) will fail. To accept variable number of arguments you should remove the parens.

      Thank you very much.

      I have changed the sub handle_other to
      sub handle_other { my $handle = shift; my $body = $handle->as_string; }
        Will it be good approach that I have used in above code ?