Script to automaticly get emails from pop3 servers using getmail allows multiple downloads, auto search.

Requirements:

  • Perl, Perl Regexp::Trie and Net::POP3. Both can be easly installed via cpan interface or Download from here
  • Create a special user account called downmx
  • Requires skeleton file (default to skel.txt ) to auto build getmail configuration scripts
  • skel_file.txt
    [retriever]
    type = SimplePOP3Retriever
    server = var1
    username = var2
    port = 110
    password = var3
    use_apop = false
    timeout = 90
    delete_dup_msgids = true
    dont-delete = true
    [destination]
    type = Maildir
    path = /home/Mailbox/var4/

  • Edit script to point to Required text files.
  • Install getmail : apt-get install getmail4 or from here
Version : 3.0
The basic functions are:
 1. Open .txt file and read parameters, User,Pass,MailServer
Example of text file:
users.txt   - ( username@domain.com,password123,mail.domain.com ) - credential file
phrases.txt - ( important n trips n lost ) - one word per line, these are key words to search for!
 2. Test if mail server and credentials, if ok save to file popOK.txt 
#TO IMPROVE -> SHOULD COUNT HOW MANY EMAILS, IF 0 DONT START DOWNLOAD (avoid creating empty folders)
 3. Download mailbox using getmail
#TO IMPROVE ->  SHOUOLD BE IN QUITE MODE, AND ADD FILTERS, NO ATTACHMENTS
 3.a Create the config file and the folders,
 4. Search Strings in Mailbox
#TO IMPROVE ->  WHEN FINDS MATCHS, CP EMAILS TO INTERSTING FOLDER

So far Its my coolest script in Perl:
Download Script

Thanks once again to Charles Cazabon who developed getmail in python.

Here is the script:

#!/usr/bin/perl -w
# Script to automaticly get emails from pop3 servers using getmail
# Author: Felipe Ferreira Date: 14/11/08, UP 20/11/08
# Version : 3.0
# 1. Open .txt file and read parameters, User,Pass,MailServer
# 2. test if mail server and credentials, OK
#### SHOULD COUNT, IF 0 DONT START DOWNLOAD (avoid creating empty folders!)
# 3. Download mailbox using getmail, OK
#### SHOUOLD BE IN QUITE MODE
# 3.a Create the config file and the folders
# 4. Search Strings in Mailbox
#### WHEN FINDS MATCHS, CP EMAILS TO INTERSTING FOLDER
#use strict;
use warnings;
use Regexp::Trie;
use Net::POP3;
my $fileSkel = "new($mailserver,Timeout => 5) or ($testpop = 1);
	if ($testpop ne 1) {
	  $tot_msg = $pop3->login($user,$pass) or ($testpop = 1);
	}
	if ($tot_msg gt 0) {
	#  printf("\n->There are $tot_msg messages\n\n");
	#Log to Text file
	open FILEOUT, "+>>", "/scripts/popOK.txt" or die $!;
		print FILEOUT $user . ";" . $pass .";". $mailserver .  "\n";
		close FILEOUT;
		$countfound++;
	  $testpop = 0;
	} else {
	  $testpop = 1;
	}
$tot_msg = 0;
}
#creates folders, and download emails
sub downloadmx()
{
	my @lines;
	my $line;
	#Must get only left part of email, to create folders.
	if ($user =~ /@/) {
		$user =~ s/@/\@/;
		(split "@", $user);
		$userP = $_[0];
	} else {
		$userP = $user;
	}
	$homeMXfolder = "/home/Mailbox/".$userP;
	#Create Folder structure where to Download
	system("mkdir $homeMXfolder");
	system("mkdir $homeMXfolder/cur");
	system("mkdir $homeMXfolder/tmp");
	system("mkdir $homeMXfolder/new");
	my $fileout = $homeMXfolder."/config.txt";
	##Open skeleton file in Read mode and parse it
	open FILE, $fileSkel  or die $!;
	@lines = ; #assigns lines to array
	close FILE;
	open FILEOUT, "+>".$fileout or die $!;
	foreach $line (@lines) #go through each line in file
	{
	#regex to substitute var1,var2,var3 to server,user,pass
		$line =~ s/var1/$mailserver/g;
		$line =~ s/var2/$user/g;
		$line =~ s/var3/$pass/g;
		$line =~ s/var4/$userP/g;
		print FILEOUT $line;
	}
	close FILEOUT;
	print "\n saved to: ". $fileout."\n";
	$homeMXfolder = $homeMXfolder."/";
	print "Downloading mails to: " . $homeMXfolder ."\n";
	#set folder perimssions
	system("chown downmx -R /home/Mailbox/");
	#download using downmx user account, since from root its not allowed
	system("su -c 'getmail --rcfile $fileout --getmaildir $homeMXfolder' downmx");
}
#Search for strings inside \new DIR
sub search() {
	my $Mcounter = 0;		# Matches Found counter
	my $Fcounter = 0;		# Files Searched counter
	my $REGEX2 = "\b(?:\\d[ -]*){13,16}\b";
	#my $homeMXfolder = "/home/Mailbox/ogigant81/new/";
	my @allfiles;
	my $file;
	my $filename;
	my @Slines;
	my $Sline;
	$homeMXfolder = $homeMXfolder."new/";
#should do a for loop in entire directory
	@allfiles = `ls $homeMXfolder`;
        foreach $file (@allfiles)
	{
	$filename = "<". $homeMXfolder . $file;
#print "Searching in: ".$filename ."\n";
		open SEARCHFILE, $filename  or die $!;
		@Slines = ; #assigns lines to array
		foreach $Sline (@Slines)
		{
		    if (($Sline =~ qr(\b$REGEX\b)) || ($Sline =~ /$REGEX2/))
		    {
			print "Match in: " .$filename. "\n";
			print $Sline ."\n";
			print "_______________________________________________________________________________\n";
			$Mcounter++;
			#SHOULD CP EMAILS TO ANOTHER FOLDER CALLED INTERESTING
		    }  # each regex MATCHES found
		 }     # each LINE loop
	close SEARCHFILE;
	$Fcounter++;
	} # loop thru each file in DIR
	print "\n  Found " . $Mcounter . " matches in ". $Fcounter. " files. \n";
	#CLEAN UP
	#$homeMXfolder = "";
}
#Get words from txt file and make list of words to search
sub buildregex()
{
	my $rt = Regexp::Trie->new;
	open(IN, "/scripts/phrases.txt");
	my @list = ;
	for (@list){
	    chomp;
	    $rt->add($_);
	}
	$REGEX =  $rt->regexp;
	print "$REGEX\n";
}
#Open and parse the files with user,server,pass and set value to global variables
sub getinfo()
{
	my @vals;
	print "staring...\n Opening " .$fileInfo."\n";
	#open the file in Read mode and parse it
	open FILEINFO, $fileInfo  or die $!;
		while () {
	#Parse using Regex to get values and print out to file
		 if (/,/) {
	          print "Line:" . $_;
		  @vals = split /,/;   	#better coding would be assign to a var
	#print "Number of values splited: " . $#_ . "\n";
		 # if (scalar @vals  eq 2) {
	  	 #  print "First Value: " . $vals[0], "\n";
	  	  # print "Second Value: " . $vals[1], "\n";
	  	   #print "Third Value: " . $vals[2], "\n";
	#Set variables
		   $user =  $vals[0];
		   $pass =  $vals[1];
		   $mailserver =  $vals[2];
	# remove leading whitespace, and remove trailing whitespace
		   $user =~ s/^\s+//;
		   $user =~ s/\s+$//;
		   $pass =~ s/^\s+//;
		   $pass =~ s/\s+$//;
		   $mailserver =~ s/^\s+//;
		   $mailserver =~ s/\s+$//;
		   $testpop = -1;
		   poptest();
		   if ($testpop eq 0) {
		    print "---Login OK---\n";
	#START DOWNLOADING
		    main();
		   } else {
		    print "LOGIN ERROR\n";
		   } # test if POP server/pass is OK
		  }
	       }  # close while
	close FILEINFO;
	print "Found: ". $countfound . " good pop3 logins\n";
}
##################################### SCRIPT START, CALLS ####################################################
sub main()
{
	downloadmx();
	buildregex();
	search();
}
#SCRIPT START HERE!
getinfo();
###################################### END ###########################################
Tags: , , , , , , , , , , , , , , , ,

4 thoughts on “Script to automatically get emails from pop3

  1. Had to copy script.pl from web source code to make it work. Works like a charm. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *