#!/usr/bin/perl # # Check and print POP3 mail. # # &grabmail("username","password","server","deliverto"); # ################### sub grabmail { my ($popuser,$poppass,$server,$deliverto,$debug) = @_; $output = "|/var/qmail/bin/qmail-inject -a $deliverto"; open (OUT,$output); my $port = 110; my $AF_INET = 2; my $SOCK_STREAM = 1; my (@hostent,$result); my $sockaddr = 'S n a4 x8'; @hostent = gethostbyname($server); my $that = pack($sockaddr, $AF_INET, $port, $hostent[4]); if (socket(POP, $AF_INET, $SOCK_STREAM, 0)) { if (connect(POP, $that)) { print "+ success: Connect\n" if ($debug); select(POP); $| = 1; select(STDOUT); $|=1; print POP "user $popuser\n"; print POP "pass $poppass\n"; until ( =~ /\n/) { print $_ if ($debug); } print POP "list\n"; my $stop = 0; while ($stop == 0) { $line = ; print "$line" if ($debug); $line =~ s/\n//g; $line =~ s/\r//g; if ($line =~ /^\d+/) { $emailcount = push(@emails,$line); } if ($line =~ /\./) { $stop = 1; } } print "+ total emails: $emailcount\n" if ($debug); foreach $email (@emails) { $email =~ /(\d+)/; print "+ getting $1\n" if ($debug); print POP "retr $1\n"; $stop = 0; while ($stop == 0) { $eline = ; print OUT $eline; if ($eline =~ /^\./) { $stop = 1; } } #print POP "dele $1\n"; } print OUT " + $emailcount emails total displayed\n"; } } close(OUT); close(POP); } return(1);