The following code should read the mail in my inbox, add it to a mysql database, and then delete it. However, I do not have the MHonArc module installed to run it. That module is used to strip html code(from what I read). All of the emails I get at the address I want to log will be undeliverable email messages telling me an email doesnt exist, so I dont really think I need the thing to strip the html, all I really need to find is the false email address. How can I modify the code below so it doesnt need MHonArc. Thanks (also, I dont know perl well, at all, just thought I would mention this)
The script is from
code:--------------------------------------------------------------------------------
#!/usr/bin/perl
#OTHER REQUIREMENTS:
use Mail::Util qw(read_mbox);
use DBI;
#-- CONFIGURE to match your database settings
my $dsn = "DBI:mysql:tech";
my $user = "--username--";
my $pass = "--password--";
#-- CONFIGURE to match ypur mailbox
my $mailfile = "/var/spool/mail/tech";
my $tmpfile = "/tmp/mk_tmp001.msg";
# mhonarc settings
my $mhcmd = "/usr/bin/mhonarc";
my $mhopt = "-quiet -single";
# check for file
if (-f $mailfile)
{
$db = DBI->connect($dsn, $user, $pass);
# parse all messages into array
@msgs = read_mbox($mailfile);
#process each message individually
foreach $msg (@msgs)
{
my $htmlmsg = ""; my $from="";
my $subject=""; my $date="";
# write out temp file
open (OUT, ">$tmpfile"
;
print OUT @$msg;
close(OUT);
# use mhonarc to convert to html
$htmlmsg = `$mhcmd $mhopt $tmpfile`;
# PREPARE FOR INSERT INTO DATABASE
#-- Clean up mhonarc stuff
#-- remove header stuff... mhonarc wants to spit out
#-- a full HTML page... I don't want that
$htmlmsg =~ s/<!--X-Subject-Header-Begin-->(.*?)<!--X-Subject-Header-End-->//s;
$htmlmsg =~ s/<html>(.*?)<body>//s;
$htmlmsg =~ s/<!--X-Head-Body-Sep-Begin-->(.*?)<!--X-Head-Body-Sep-End-->//s;
$htmlmsg =~ s/<!DOCTYPE(.*?)">//;
#-- parse Header for from, subject and date
if ($htmlmsg =~ m/<!--X-Head-of-Message-->(.*?)<!--X-Head-of-Message-End-->/s)
{
my $header = $1;
if ($header =~ m/<em>From<\/em
.*)/) { $from = $1; }
if ($header =~ m/<em>Subject<\/em
.*)/) { $subject = $1; }
if ($header =~ m/<em>Date<\/em
.*)/) { $date = $1; }
$htmlmsg =~ s/<!--X-Head-of-Message-->(.*?)<!--X-Head-of-Message-End-->//s;
}
# remove all HTML comments
$htmlmsg =~ s/<!--(.*?)-->//g;
# - double up apostrophes
$htmlmsg =~ s/'/''/g;
# INSERT INTO DATABASE
$sql = " INSERT INTO techpage ";
$sql = $sql . " (sender, subject, description) VALUES ";
$sql = $sql . " ('$from','$subject','$htmlmsg') ";
$db -> do($sql);
}
$db->disconnect;
#-- THIS IS COMMENTTED OUT FOR YOUR PROTECTION
#-- The mailbox file should be deleted after reading
#-- it so the same message doesn't keep getting inserted
#-- into the database. I comment it out so during testing
#-- the mail is not lost
# remove mail file
#unlink($mailfile);
}
JoeM6
JPMJR11@aol.com
The script is from
code:--------------------------------------------------------------------------------
#!/usr/bin/perl
#OTHER REQUIREMENTS:
use Mail::Util qw(read_mbox);
use DBI;
#-- CONFIGURE to match your database settings
my $dsn = "DBI:mysql:tech";
my $user = "--username--";
my $pass = "--password--";
#-- CONFIGURE to match ypur mailbox
my $mailfile = "/var/spool/mail/tech";
my $tmpfile = "/tmp/mk_tmp001.msg";
# mhonarc settings
my $mhcmd = "/usr/bin/mhonarc";
my $mhopt = "-quiet -single";
# check for file
if (-f $mailfile)
{
$db = DBI->connect($dsn, $user, $pass);
# parse all messages into array
@msgs = read_mbox($mailfile);
#process each message individually
foreach $msg (@msgs)
{
my $htmlmsg = ""; my $from="";
my $subject=""; my $date="";
# write out temp file
open (OUT, ">$tmpfile"
print OUT @$msg;
close(OUT);
# use mhonarc to convert to html
$htmlmsg = `$mhcmd $mhopt $tmpfile`;
# PREPARE FOR INSERT INTO DATABASE
#-- Clean up mhonarc stuff
#-- remove header stuff... mhonarc wants to spit out
#-- a full HTML page... I don't want that
$htmlmsg =~ s/<!--X-Subject-Header-Begin-->(.*?)<!--X-Subject-Header-End-->//s;
$htmlmsg =~ s/<html>(.*?)<body>//s;
$htmlmsg =~ s/<!--X-Head-Body-Sep-Begin-->(.*?)<!--X-Head-Body-Sep-End-->//s;
$htmlmsg =~ s/<!DOCTYPE(.*?)">//;
#-- parse Header for from, subject and date
if ($htmlmsg =~ m/<!--X-Head-of-Message-->(.*?)<!--X-Head-of-Message-End-->/s)
{
my $header = $1;
if ($header =~ m/<em>From<\/em
if ($header =~ m/<em>Subject<\/em
if ($header =~ m/<em>Date<\/em
$htmlmsg =~ s/<!--X-Head-of-Message-->(.*?)<!--X-Head-of-Message-End-->//s;
}
# remove all HTML comments
$htmlmsg =~ s/<!--(.*?)-->//g;
# - double up apostrophes
$htmlmsg =~ s/'/''/g;
# INSERT INTO DATABASE
$sql = " INSERT INTO techpage ";
$sql = $sql . " (sender, subject, description) VALUES ";
$sql = $sql . " ('$from','$subject','$htmlmsg') ";
$db -> do($sql);
}
$db->disconnect;
#-- THIS IS COMMENTTED OUT FOR YOUR PROTECTION
#-- The mailbox file should be deleted after reading
#-- it so the same message doesn't keep getting inserted
#-- into the database. I comment it out so during testing
#-- the mail is not lost
# remove mail file
#unlink($mailfile);
}
JoeM6
JPMJR11@aol.com