# Status - Program flow seems to be correct. Program IO seems correct. Program connectivity seems correct. # Issue with string evaluation. Program will not return a match if there if anything MORE than a correct match... # i.e. - if $SearchTerms = "blue monkies" and the only thing in the body of the email is "blue monkies" it returns # a match. But if the body of the email reads "maroon baboons are not blue monkies" no match is returned. # 1/17/2012 - JLC # MailCrawl uses 3 arguments: -h -u -p # There is a hard coded path to Search_Terms.txt assigned to $InFile. This is the file containing search terms. # Needs to be a standard text file with one search term per line. # Emails matching the search criteria are saved in a raw text file Email..txt. Path is hard coded to $OutFile #!/bin/perl # import packages use Net::POP3; use Getopt::Long; use strict; use warnings; # Declare variables my $Sec; my $Min; my $Hour; my $MDay; my $Mon; my $Year; my $WDay; my $YDay; my $IsDst; my $Today; my $InFile; my $OutFile; my @SearchTerms; my $Host; my $User; my $Pass; my $NumMsg; my $MsgCount; my $SearchTerms; my $Msg; my $MsgList; my $TermNum; my $TermCount; my $Ref; # Get todays date to be used in output file name ($Sec,$Min,$Hour,$MDay,$Mon,$Year,$WDay,$YDay,$IsDst) = localtime(time); $Year += 1900; $Mon++; $Today = "$Mon.$MDay.$Year"; # Set file paths $InFile = 'H:\Tools\MailCrawl\Search_Terms.txt'; $OutFile = 'H:\Tools\MailCrawl\Email_Files\Email.'. $Today . '.txt'; # Load search terms from file into @SearchTerms array open (SearchTerms, "<$InFile") or die "(ERROR: Unable to open Search_Terms).\n"; @SearchTerms = ; close (SearchTerms); # read command line options # display usage message in case of error GetOptions ('h|host=s' => \$Host, 'u|user=s' => \$User, 'p|pass=s' => \$Pass) or die("Input error. Try calling me with: -h -u -p "); # initiate connection # default timeout = 120 sec my $conn = Net::POP3->new($Host) or die("ERROR: Unable to connect.\n"); # login $NumMsg = $conn->login($User, $Pass) or die("ERROR: Unable to login.\n"); # $MsgCount used to show current message being evaluated $MsgCount=0; # get message numbers # iterate over list and print each if ($NumMsg > 0) { $MsgList = $conn->list(); # Loop for each email msg foreach $Msg (keys(%$MsgList)) { # Set variables to be displayed to show search status $TermNum = @SearchTerms; $TermCount = 0; $MsgCount++; $Ref = $conn->get($Msg); # Loop for each search term for $SearchTerms (@SearchTerms){ $TermCount++; print "searching msg $MsgCount of $NumMsg for $SearchTerms...\n"; # If match was found, write email to file if (@$Ref ~~ /$SearchTerms/i) { open (EmailFile, ">>$OutFile"); print EmailFile "@$Ref \n"; close (EmailFile); } } } } else { print "Mailbox is empty.\n"; } # close connection $conn->quit();