For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > MIME::Parser









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author MIME::Parser
David Foley

2005-08-08, 9:04 am

Hi Guys,
I need help with MIME::Parser. I have a script downloading
mail, it takes the attachments and saves them with MIME::Parser. It uses
the filename it gets from the header. I need to save it with a custom
filename though. How do I do this. Below is the script so far.

#!/usr/bin/perl -w
# This script was created by David Foley (rw5@eircom.net)

#Reduce scripting errors
use strict;

#Call relevant modules
use Net::POP3;
use MIME::Parser;

#Connect to POP3 server - 82.195.128.132
my $pop3server = Net::POP3->new('82.195.128.132');

#Login to POP3 server - 82.195.128.132
$pop3server->login('*****userpass******);

#How many messages there is
my $lastm = ($pop3server->popstat)[0];

#Get and process mail
for my $messageID (121){

#Get Message
my $MFH = $pop3server->getfh($messageID);

#Read the messages into '$msg'
my $msg = '';
$msg .= $_ while <$MFH>;

print "$msg";

#Create new MIME::Parser
my $MIMEparser = new MIME::Parser;
$MIMEparser->output_dir( 'lics' );
$MIMEparser->output_path('$fname = BATMAN');

#Create MIME::filer
#my $MIMEfiler = $MIMEparser->filer;

#$MIMEfiler->output_filename('BATMAN');

#And parse eMail
$MIMEparser->parse_data($msg);

};


--

*David Foley*
Reflex IS, Ireland
david@reflex.ie <mailto:david@reflex.ie>

tel: +353 51 841051
fax:+353 51 841052
http://www.reflex.ie



*Antivirus protection:Can you afford less than the best?*

Highest Tested





Detection Rate

Fastest Scanning Rate





Lowest Memory Use

New Advanced Heuristics





Detects Unknown Viruses

Automatic Internet Updates





Easy Administration

NOD 32 <http://www.nod32.ie>

Thomas Bätzler

2005-08-08, 9:04 am

David Foley <david@reflex.ie> asked:
> I need help with MIME::Parser. I have a script
> downloading mail, it takes the attachments and saves them
> with MIME::Parser. It uses the filename it gets from the
> header. I need to save it with a custom filename though. How
> do I do this. Below is the script so far.


You will have to derive a class from MIME::Parser::FileInto
that implements the the naming scheme you want. You can define
your class inside your script like this:

package MIME::Parser::FileInto::Myparser;

use 5.006;
use strict;
use warnings;

require MIME::Parser::Filer;
require MIME::Head;

our @ISA = qw(MIME::Parser::FileInto);

sub init_parse {
# called once when the parsing process starts
# put init stuff here
}


sub output_path {

my $self = shift;

my $path = $self->SUPER::output_path( @_ );

# mangle output path/filename here

return $path;
}

1;

package main;

# set up MIME::Parser object and tell it to use your custom
# filer code

my $parser = MIME::Parser->new();
my $filer = MIME::Parser::FileInto::Myparser->new($tempdir);

$parser->filer( $filer );

# run parse
my $entity = eval { $parser->parse(\*STDIN) };

__END__

HTH,
Thomas
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com