IvyR has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to parse some e-mail. I started with one and it came back MIME encoded. So I went poking around online for an hour or so and found a tutorial that set up a group of use statements and then continued on. I wanted to make certain the modules were installed, so I ran after typing in the using.
Then, as if by magic, the message was plain text. I thought it was something else I'd done, so I commented out those using statements. Still plain text. So I did the stupid thing and deleted them.
Now I've put two more messages in the box and they come back MIME encoded. Any clue what the magic using statements are? I've attached my code (such that it is) below.
#!/usr/bin/perl use Net::POP3; use strict; use warnings; use diagnostics; use DateTime; use XML::Writer; #DateTime and XML::Writer are not installed by default. #Notes: # If for some insane reason this becomes more than throwaway code and + isn't redone in C# # I want to break the component parts out to methods and move the reg +ex/value pairs to a config file. # I also need to do more with error handling than send a message to S +TDError. my $rundate = DateTime->today; # Prepare to write the XML. my $xmloutput = new XML::Writer( ); $xmloutput->xmlDecl('UTF-8'); $xmloutput->startTag( 'CaseList', 'transactiondate'=>$rundate); #The +root element my $pop = Net::POP3->new('pop.secureserver.net', Timeout => 60); $pop->login('alm@ivyreisner.com','xxxx') > 0 or die "Cannot log into e +-mail"; my $msgnums = $pop->list or die "Cannot fetch messages"; foreach my $msgnum (keys %$msgnums) { $xmloutput->startTag("Case"); my $payload; my $subject; my $rundate = DateTime->today; $xmloutput->dataElement("Subject", $subject); #Now that we have the messages in an array, we can parse the one a +t a time. my $msg = $pop->get($msgnum) or die "Cannot read message " + $msgn +um; foreach my $msgline (@$msg) { if ($msgline =~ /^Subject:/) { $subject = $msgline; $subject =~ s/^Subject: //; chomp($subject); #chomp = trim } elsif ($msgline =~ /Case Title:/) #Just finding the character +s that tell me I'm in the body for sure. { $payload = $msgline; if ($payload =~ m|(Index #: </b>)(.*)(<br><b>Case Title)|) { $xmloutput->dataElement("Index Number", $2); } if ($payload =~ m|(Case Title: </b>)(.*)(<br><b>Venue)|) { $xmloutput->dataElement("Case Title", $2); } if ($payload =~ m|(Venue: </b>)(.*)(<br><b>Court)|) { $xmloutput->dataElement("Venue", $2); } if ($payload =~ m|(Court: </b>)(.*)(<br><b>Date Filed)|) { $xmloutput->dataElement("Court", $2); } if ($payload =~ m|(Date Filed: </b>)(.*)(<br><b>Date enter +ed)|) { $xmloutput->dataElement("Date Filed", $2); } if ($payload =~ m|(Date entered: </b>)(.*)(<br><b>Document + Type)|) { $xmloutput->dataElement("Date Entered", $2); } if ($payload =~ m|(Document Type: </b>)(.*)(<br><b>Filing +Party)|) { $xmloutput->dataElement("Document Type", $2) } if ($payload =~ m|(Filing Party: </b>)(.*)(<br><b>Attorney +)|) { $xmloutput->dataElement("Filing Party", $2) } if ($payload =~ m|(Attorney: </b>)(.*)(<br><b>Order Outcom +e)|) { $xmloutput->dataElement("Attorney", $2) } if ($payload =~ m|(Order Outcome: </b>)(.*)(<br><b>Comment +s)|) { $xmloutput->dataElement("Order Outcome", $2) } if ($payload =~ m|(Comments: </b>)(.*)(<o:p>)|) { $xmloutput->dataElement("Comments", $2); } } } $xmloutput->endTag(); } $pop->quit; $xmloutput->endTag(); $xmloutput->end();
Thank you very much.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parsing mime encoded e-mails
by Khen1950fx (Canon) on Sep 10, 2011 at 02:18 UTC |