Fellow monks: the situation came up that i needed to write an interface that slurps HTML formated IMAP4 emails and spits out syslog entries. I decided to use the modules included below. It seems to work great, as long as there's only one message in the INBOX. Any more than that, and each log entry includes all the previous. Anything in my code below not re-initializing properly, or is Email::Simple returning the current message body in addition to all the previous? I've included some of my own debugging lines. (FYI it's been about 3 years since the last time i wrote any code)

#!/usr/local/bin/perl use Net::IMAP::Simple; use Email::Simple; use Data::Dumper; use HTML::Stripper; use Sys::Syslog; my $user = 'someuser'; my $password = 'somepass'; my $mailhost = 'somehost'; my $server = Net::IMAP::Simple->new($mailhost) || die "No server\n"; $server->login($user, $password) || die "Access denied\n"; my $number_of_messages = $server->select('INBOX'); foreach my $msg ( 1 .. $number_of_messages ) { #UPDATED STRIPPER INITIALIZATION LOCATION my $stripper = HTML::Stripper->new( skip_cdata => 1, strip_ws =>0 );</B> my $email = Email::Simple->new( join '',@{$server->get( $msg )} ); my $newline = ""; $_ = $email->header('Subject'); if(/^Site ID\:/){ ### IF IT'S A "Site ID:" MSG... my $html_message = $email->body; my $txt_message = $stripper->strip_html($html_message); my @email_array = split /\cM/, $txt_message; foreach my $line (@email_array){ $line =~ s/\n//g; if(($var, $value) = $line =~ /^\s*(.+\S+\s*): (.*)/){ $var =~ s/\s/\_/g; ## convert spaces in key to unders +core $value =~ s/\s*$//g; ## zap trailing spaces from valu +e $newline = $newline.$var."=\"".$value."\" "; $var, $value = ""; } ### end search for var, value $line = ""; $_ = $newline; } ### end processing of each line my $line_length = length $newline; # print "LINE: $newline\n"; # print "$line_length"; $newline =~ /.{,923}/; ## syslog can only handle 1024 characte +rs, so chop some $newline =~ s/\s*$//g; ## zap trailing spaces # my $new_length = length $newline; # print ":$new_length"; $_ = $newline; if(!$newline =~ /\"$/){ ## If the trailing character is NOT a + " $newline = $newline."\""; } ### END MSG LENGTH CHECK #openlog($0,'nowait,pid',local7); #syslog('local7|notice',, $newline); #syslog('local7|notice',,'flush'); #closelog(); print "LINE:$newline\n-----------------\n"; $newline = ""; # $last_newline = length $newline; if($server->copy($msg, 'Processed')){ $server->delete($msg); } } ### END IF SITE # sleep 1; } ### END FOREACH MSG $server->quit();

Thanks!


In reply to Email::Simple doing something i'm not expecting?? by eLore

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.