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

hi all , am new in perl and I am modifying a perl script(see below).... and am trying to use the MIME mode to extract a txt file..... I' m having some problems with the Parser, i have an email coming in that i want to convert to csv text ... is coming as text/plain.... format and also some other formats like text html etc..... how can I get the file to csv format? This is the file that am trying to convert Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US, en-AU Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 MIME-Version: 1.0 DQoNClRlY2hub2xvZ3kgTWFuYWdlcg0KSVAgQ2FsbGluZyBTb2x1dGlvbnMgYW5kIENQRSBNYW5h Z2VtZW50DQpDb252ZXJnZWQgQ2FsbGluZyBhbmQgVUMgU29sdXRpb25zDQo2MSAoMCkzIDg2NDcg
#! /usr/bin/perl open FILE,">>/tmp/SRP_RMS.xlog"; print FILE "\nInitialising\n"; #filename = $ARGV[0]; my $dbh = DBI->connect($dsn,$user,$pass) or die "Can't Connect to the DB: $DBI::errstr\n"; #open FILE, $filename; $indata=0; #$csv = Text::CSV->new(); #$processed = 0; use MIME::Parser; $parser = new MIME::Parser; $output = "/tmp/mimex"; $parser->output_dir($output); my $entity = $parser->read(\*STDIN); my $num_parts = $entity->parts; if($num_parts > 0) { for(my $i = 0; $i < $num_parts;$i++) { my $part = $entity->parts($i); my $type = lc $part->mime_type; my $bh = $part->bodyhandle; print FILE "Type: ".$type."\n"; if($type eq 'text/plain') { print FILE "processing: ".$bh->{MB_Path}."\n"; open CSVFILE, "<$bh->{MB_Path}"; #$csv = Text::CSV->new(); #$processed = 0; while (<CSVFILE>) { $line = $_; if($line =~ /^$/) { } elsif ($indata ==0)

Replies are listed 'Best First'.
Re: Perl MIME
by Tux (Canon) on Mar 15, 2012 at 14:33 UTC

    And obfuscate your user credentials. And now change your root password.


    Enjoy, Have FUN! H.Merijn
      I removed the user credentials, thanks for that.

        The code tags help. Now you should use perltidy to make it show as real code :)

        Maybe you can elaborate on the error you get, as the code you posted is pretty long, and the "problem" you are facing is not well-enough defined for us to make a good advice or analysis.


        Enjoy, Have FUN! H.Merijn
Re: Perl MIME
by molecules (Monk) on Mar 15, 2012 at 13:51 UTC
    Please change the last code tag to be </code> so that it will display properly.