Well,
Background: I'm writing a script to send a notification mail to an e-mail
address based on an incoming mail. I'm using procmail to decide which
messages should be notified about and to pipe them one by one into said
script.
[edit]I wish I could perform edits like in the forums ;-)[/edit]
Here's what I've got so far. It mostly works but a couple of things left to
do. Obviously, I still have to send the notification mail out. I'll try to do
that in Perl but will probably end up using mailx so I don't have to bother
with the mail headers. I also have to detect whether there are several MIME
sections and deal with that - if there's a plain text one grab that else use
the html. Those things I should be able to do by myself.
#!/usr/bin/perl
use Mail::Internet;
use HTML::Parser;
$mail = Mail::Internet->new(*STDIN);
$mail_head = $mail->head();
$msg_date = $mail_head->get("Date");
$msg_from = $mail_head->get("From");
$msg_subj = $mail_head->get("Subject");
$mail->tidy_body();
$mail_body_ref = $mail->body();
@mail_body = @$mail_body_ref;
@body_text = "";
$body_pars = HTML::Parser->new(
text_h => [sub
{
$text = shift;
if ($text =~ /[0-9a-zA-Z]/) {
@body_text = (@body_text, $text);
}
}, 'dtext']);
foreach (@mail_body) {
$body_pars->parse("$_\n");
}
$body_pars->eof();
$ntfy_head = Mail::Header->new();
$ntfy_head->add("From:", "[EMAIL PROTECTED]");
$ntfy_head->add("To:", "[EMAIL PROTECTED]");
$ntfy_head->add("Subject:", "Mail Notification");
$ntfy = Mail::Internet->new(Header => $ntfy_head);
@ntfy_mail = ("Date: $msg_date",
"From: $msg_from",
"Subject:$msg_subj",
"\n", @body_text[1..5], "\n");
$ntfy->body(@ntfy_mail);
$ntfy->print();
What I am having problems with is getting around a known bug in HTML::Parser.
It doesn't deal with embedded style sheets in HTML (or however they're
correctly termed). Does anybody have any suggestions on how I can get around
that? The mail I was using to test the script was a "Core Java Technologies
Newsletter" but running any mail with embedded HTML that uses an embedded
style sheet will show up what I mean.
The only other thing which I haven't had time to really check into yet - too
much time spent reading man pages! - is that it seems 1 element of @bodytext
has more than one line of text; that is to say, each of element of @bodytext
has >=1 newline. This results in @bodytext[1..5] returning about 30 lines of
text with the test email I was using. How can I use perl to get just 5 (or
10) lines of text?
Regards,
Jason
--
[EMAIL PROTECTED] mailing list