Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using mail2mysql without MHonArc?

Status
Not open for further replies.

JoeM6

Programmer
Aug 5, 2000
53
0
0
US
gamecentral.20m.com
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(.*?)&quot;>//;


#-- 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:mad:.*)/) { $from = $1; }
if ($header =~ m/<em>Subject<\/em:mad:.*)/) { $subject = $1; }
if ($header =~ m/<em>Date<\/em:mad:.*)/) { $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 = &quot; INSERT INTO techpage &quot;;
$sql = $sql . &quot; (sender, subject, description) VALUES &quot;;
$sql = $sql . &quot; ('$from','$subject','$htmlmsg') &quot;;
$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
 
first off some be very cautious doing this from what I have read it can be a nasty monster

make a new account for bad mail so you can forward them:

You can use LUSER_RELAY in *.mc file you use to generate your sendmail.cf
file to relay messages to unknown local users.
e.g. define(`LUSER_RELAY',`local:postmaster+luser')

hook your mySQL to this account
anyway maybe a starting point if this is not what you are looking for
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top