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

I have a script which reads certain emails that are piped to it.

Here is the part that successfully gets the "body" of the message:
if ($MIME_entity->parts > 0) { for (my $i=0;$i<$MIME_entity->parts;$i++) { my $subEntity = $MIME_entity->parts($i); my $ignore_plain = 0; my $ignore_html = 0; $ishtml = "1" if $subEntity->mime_type eq 'text/html'; $ishtml = "0" if $subEntity->mime_type eq 'text/plain'; if (($subEntity->mime_type eq 'text/html') && ($ignore_htm +l == 0)) { if (my $io = $subEntity->open("r")) { while (defined($_=$io->getline)) { $_ =~ s/"/\"/g; $body .= $_; } $io->close; $ignore_plain=1; } } if (($subEntity->mime_type eq 'text/plain') && ($ignore_pl +ain=0)) { if (my $io = $subEntity->open("r")) { while (defined($_=$io->getline)) { $_ =~ s/"/\"/g; $body .= $_; } $io->close; $ignore_html=1; } } } } else { $body = join "", @{$MIME_entity->body}; }
My question is this...
Why can't I split the body, when trying to get a certain piece of info?
Here is how I'm trying it. I first check the subject and body.
if (($to && ($to =~ /system_mail\@/)) && ($subject =~ /(mail failu +re|mail delivery failure|Mail delivery failed)/i && $body =~ /permane +nt error/i)) { Set_Mail_Failure($newfrom,$subject,$body); }
The purpose is to know if a registered user has canceled their email account, and has not updated it in our database. This way we do not send them any more to that email account, until they re-add that account and re-validate it.

Here is what I did with the Set_Mail_Failure sub.

sub Set_Mail_Failure { my ($_c_from,$_c_subject,$_c_body) = @_; $_temp_body = $_c_body; $_cntr = 0; foreach my $_lck (split /\015\012/, $_temp_body) { $_cntr++; $_lck =~ s/^\s+$//g; $_lck =~ s/\s+$//g; if ($_lck =~ /^To:/i) { ($_trash,$_actual_email) = split /\</, $_lck, 2; ($_actual_email,$_trash) = split /\>/, $_lck, 2; $_is_right_chk = $dbh->selectrow_array(qq{ SELECT `subscri +bed` FROM `reg_users` WHERE `email` = ? }, undef, $_actual_email); if ($_is_right_chk) { $_full_msg = $_c_subject . "\n\n" . $_c_body; $dbh->do (qq{ UPDATE registered_users SET subscribed = + ?, mail_failure = ?, mail_failure_msg = ? WHERE email = ?}, undef, 0, 1, $_full_msg, $_actual_email); open(FILE,">>show_results.txt") or die "Could not open + file... $!"; # Debug only print FILE "Found email to be $_actual_email - Now I'm + ending this and exiting...\n"; # Debug only close(FILE); # Debug only last; $dbh->disconnect() if $dbh; exit; } else { next; } } else { next; } } # Debugging only.... not going to stay. open(FILE2,">show_email.txt") or die "Could not open file... $!"; print FILE2 "$_c_body"; close(FILE2); open(FILE,">>show_results.txt") or die "Could not open file... $!" +; print FILE "I must have NOT found the email($_cntr lines)! I am fi +xing to exit!\nLast Line: $_lck\n"; close(FILE); exit; # Did not work, and since it was a error email, just ignore it a +nd exit... }
I know I'm not a "good" programmer, but it does work, except the Set_Mail_Failure. Well it works, however, I cannot get the "split" to work. I've tried these different ways:
split /\015\012/, $_temp_body split /\cM\cJ/, $_temp_body split /\r\n/, $_temp_body
None of them work :o(
Those are the different way's I've tried to split the foreach statement. I even tried a while statement and that had the same results.

Can you please tell me how or what is making it not split? The debug .txt file I have it create, has $_lck contain the WHOLE email body message.

I would really appreciate some help with this.
Thanks in advance for any assistance.

Richard

Replies are listed 'Best First'.
Re: reading a email piped to a script....
by powerhouse (Friar) on Feb 07, 2004 at 02:23 UTC
    I think I might just have to run one more process, and that being insert the whole "body" of the email into a temporary database, and then just go get it from there, then parse it, that MIGHT work. What do you think? Is there a easier way?
    thx,
    Richard
      I found it.... I had never tried to just split by \n That worked!

      Just letting everyone know that I solved this issue. Thanks for reading :o)

      thx,
      Richard