Solved

More MIME-tools.

Posted on 2002-05-31
3
627 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
  • 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 200 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

786 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