MIME::Parser .. how to get just the message part of the body
am 07.09.2005 19:28:49 von matthewlenz
#!/usr/bin/perl
use MIME::Parser;
use Data::Dumper qw(Dumper);
$parser = MIME::Parser->new( );
$parser->output_to_core(1); # don't write attachments to disk
while () {
$MESSAGE .= $_;
}
$message = $parser->parse_data($MESSAGE); # die( )s if can't parse
$head = $message->head( ); # object--see docs
$preamble = $message->preamble; # ref to array of lines
$epilogue = $message->epilogue; # ref to array of lines
$num_parts = $message->parts;
for (my $i=0; $i < $num_parts; $i++) {
print "part number = $i\n";
my $part = $message->parts(1);
my $content_type = $part->mime_type;
my $body = $part->as_string;
print $body;
}
.......................
When I print the body I get the content headers as well. Is there a
way to just get the actual content?
-Matt
Re: MIME::Parser .. how to get just the message part of the body
am 10.09.2005 05:45:09 von Gerard Lanois
matthewlenz@gmail.com writes:
> #!/usr/bin/perl
>
> use MIME::Parser;
> use Data::Dumper qw(Dumper);
>
> $parser = MIME::Parser->new( );
> $parser->output_to_core(1); # don't write attachments to disk
>
> while () {
> $MESSAGE .= $_;
> }
>
> $message = $parser->parse_data($MESSAGE); # die( )s if can't parse
>
> $head = $message->head( ); # object--see docs
> $preamble = $message->preamble; # ref to array of lines
> $epilogue = $message->epilogue; # ref to array of lines
>
> $num_parts = $message->parts;
> for (my $i=0; $i < $num_parts; $i++) {
> print "part number = $i\n";
> my $part = $message->parts(1);
^
^
^
Possible source of trouble here. Maybe you meant $i???
-Gerard
> my $content_type = $part->mime_type;
> my $body = $part->as_string;
> print $body;
> }
>
>
> ......................
>
> When I print the body I get the content headers as well. Is there a
> way to just get the actual content?
>
> -Matt
Re: MIME::Parser .. how to get just the message part of the body
am 16.09.2005 21:45:25 von ivan.delchev
try this one.Incomming poarameter must be text of the message
sub get_email_body {
my $MESSAGE = shift;
use MIME::Parser;
$parser = MIME::Parser->new( );
$message = $parser->parse_data($MESSAGE); # die( )s if can't parse
$head = $message->head( ); # object--see docs
$preamble = $message->preamble; # ref to array of lines
$epilogue = $message->epilogue; # ref to array of lines
print "\npreamble:".Dumper($preamble);
print "\nepilogue:".Dumper($epilogue);
$num_parts = $message->parts;
for (my $i=0; $i < $num_parts; $i++) {
my $part = $message->parts($i);
my $content_type = $part->mime_type;
my $body = $part->as_string;
# print "\n".$content_type."\n";
# print "\n".$body."\n\n\n";
if ($content_type eq 'text/plain') {
return $part->bodyhandle->as_lines;
}
}
}