[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

More MIME-tools.

Posted on 2002-05-31
3
Medium Priority
?
633 Views
Last Modified: 2008-03-03
I would like to use mimetools to parse a message into ram, look at the attached file names, then be able to save given parts (decided by type or recommended filename) to disk. I also want to be able to give each part a new name if the file already exists.

My code as of now is below, so how do i manipulate the files _before_ actually writing them to disk?

#!/usr/bin/perl -w
use strict;
$|++;

use MIME::Parser;
use MIME::Head;

my $parser = new MIME::Parser;
$parser->output_dir("./out");

# read STDIN
my $entity = $parser->read(\*STDIN) or die "couldn't parse MIME stream";

# to get message headers:
my $head = $entity->head;
$head->decode;
#print $head->as_string;
print "\n\n";
my $to = $entity->head->get('To');
my $from = $entity->head->get('from');
my $subject = $entity->head->get('subject');
my $xat = $entity->head->get('X-Apparently-To');
chomp ($to, $subject, $from, $xat);

print "Group: $xat Subject: $subject From: $from\n";
# message parts
my $part;
foreach $part($entity->parts) {
 # do something with message part

 # prints path to extracted file
 print $part->bodyhandle->path;
 print " ";

 # prints mime-type
# print $part->head->mime_type;
# print "\n";
}
print "\n======================================================================================================================\n";
0
Comment
Question by:j2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:andreif
ID: 7050884
Hi,

I had the same problems :)

As a result, I allow to MIME parser save attachments to temporary derectory and then in the cycle
foreach $part($entity->parts) {

# procedure SuggestNewPath takes temporary file path as
# parameter, extracts pure filename, checks if this name
# is free in target directory. If not, it adds number to
# the filename

$newpath = &SuggestNewPath($part->bodyhandle->path);

# after that "$newpath" contains new path to the file
# and I move old file to the new location

move ($part->bodyhandle->path, $newpath);

}

sub SuggestNewPath {
        my ($cfn) = @_;

        my ($name, $ext);
        my $basepath = "/target/directory"

        # get pure filename (assume that it's string
        # after last slash or backslash)
        $cfn =~ s/.*(\/|\\)//g;

        if (-e "$basepath/$cfn") {
                # if file with this name exists
                # we have to give different name
                # to the new file. Split file name
                # into 2 parts: filename & extension
                if ($cfn =~ /^(.*)\./) {
                        ($name,$ext) = ($1,$');
                } else {
                        ($name,$ext) = ($cfn,"");
                }
                my $i = 0;

                # trying filenames like
                # file2.ext, file3.ext

                while (-e "$basepath/$name".++$i.".$ext") {}
                return "$basepath/$name".$i.".$ext";
        } else {
                return "$basepath/$cfn";
        }
}
0
 
LVL 12

Author Comment

by:j2
ID: 7051000
Uhm, call me dumb here, but how would i implement that in a "complete" solution? I mean, it solves the generate-new-name part in an excellent way.

I cant make headfs or tails of the mime-tools docs, i seem to be missing the "Newbie" stuff/examples as in "how to check type, size, whatever" and stuff :-/
0
 
LVL 6

Accepted Solution

by:
andreif earned 800 total points
ID: 7051767
Hi,

feel free to ask more questions if you need. When I tried MIME-Tools for the first time, it was not a piece of cake :)

MIME-Tools is a set of modules, basic is MIME::Entity which contains MIME::Head, MIME::Body

To get more info about them, follow this link
http://ls6-www.cs.uni-dortmund.de/ir/projects/SFgate/CPAN/

and search for "MIME" (by name)

Start with
"MIME-tools - modules for parsing (and creating!) MIME entities"

Then check MIME::Entity, MIME::Head - there are some examples.

Feel free to ask me if you need help (don't close this question till you get it working)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question