Script to delete email from an account

T

thawkins

I posted the below in the Perl Module usergroup by mistake. So I'm
re-posting here.

I went searching for a script that will delete emails in one of my user
accounts. I found the script below. I set it up as a crons job, which
seems to be running fine. It connects to the user account, reads the
number of messages but doesn't delete any messages. Perl is not a
language that I am familiar with. I did spend time looking at the
script and to me it should be deleting the email messages. I was
hoping someone here could look at it and tell me why it is not working.
Thanks in Advance.

# ! /usr/bin/perl -w
use strict;
use warnings;

use Net::pOP3;

my ($popserver, $user, $pass, $maxmsg, @validusers);
my ($numDeleted,$numLeft) = (0,0);
my $pop;

# run control parameters - should be in a separate file
$popserver = 'mail.anyaddress.usa'; # pop3 server address
$user='(e-mail address removed)'; # enter your
pop3 username
$pass='XXXXXXXXXX'; # and password here
$maxmsg=100; # number of
messages to process in a single batch
@validusers = qw(postmaster webmaster);
# names NOT to delete messages for
# - enter with a space between. eg: qw(bob andy terry)
#--------- end parameters.

print "connecting to server $popserver..\n";
if ($pop = Net::pOP3->new($popserver,Debug=>0)) {
print qq(Connected. Logging on as $user\n);
if (my $msgcnt = $pop->login($user,$pass)) {
if ($msgcnt > 0) {
print "$msgcnt messages....\n";
$msgcnt = $maxmsg if $msgcnt > $maxmsg; #
limit to just the max I have set
getmsg:
for my $msgnum (1..$msgcnt){
my ($hdr) = $pop->top($msgnum,0);
my @headers = (@$hdr);
my $nh = $#headers;
my $j=0;
for my $i (0..$#headers) { #
loop through the headers
$_=$headers[$i];
# putting multi-line onto one.
if (/^\S+:/) {
#print "$j
$headers[$j]\n";
$j = $i;
}
else {
$headers[$j] .=
$headers[$i];
$headers[$i] = "";
}

$headers[$j] =~ s/\s+/ /g;
# remove multiple spaces
$headers[$j] =~ s/(^ )|( $)//g;
# remove leading/trailing space
}
for(@headers) {print "$_\n";}

my $sj = "";
my @sjhdr = grep(/^Subject:/,
@headers); # get the Subject Header
if ( exists $sjhdr[0] && $sjhdr[0] =~
m/(^Subject: +)(.+)/ ) {
$sj= substr($2,0,40);
}
# print "$nh headers!\n";
my @rechdr = grep(/^Received:/,
@headers); # scan the received
for (@rechdr) {
# header for the
if
(m/(^Received:.+for\s+)(.+)(\@$user)/) { # envelope addressee
my $rcpt = $2;

for (@validusers) {
# check this recipient against
if (lc($rcpt) eq
$_){ # our list of valid users
print
qq(Leaving msg $msgnum for "$rcpt\@$user" "$sj"\n);

$numLeft++;
next
getmsg; # skip if we want to keep it
}
}

print qq(Deleting msg
$msgnum for "$rcpt\@$user" "$sj"\n);
$pop->delete($msgnum);
# otherwise delete it
$numDeleted++;
next getmsg;
}
}
}
print "$numDeleted deleted, $numLeft left\n";
}
else {
print qq(No messages on the server\n);
}
$pop->quit();
}
else {
print "Error logging on to server $popserver\n";
}
}
else {
print "Error Connecting to $popserver, $!\n";
}
sleep 5;
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
I went searching for a script that will delete emails in one of my user
accounts. I found the script below. I set it up as a crons job, which
seems to be running fine. It connects to the user account, reads the
number of messages but doesn't delete any messages. Perl is not a
language that I am familiar with. I did spend time looking at the
script and to me it should be deleting the email messages. I was
hoping someone here could look at it and tell me why it is not working.

You'll need to go to a jobs forum if you want to hire a consultant, or ask
the person from whom you got the script.

Else, please read the posting guidelines for this group.

Sinan
 
S

Sisyphus

I posted the below in the Perl Module usergroup by mistake. So I'm
re-posting here.

I went searching for a script that will delete emails in one of my user
accounts.

Here's a modified version of a script I use. This modified version is
unchecked - beware of typos and other silly errors.
The actual script I use parses the headers, and decides whether or not to
delete the email based on what the headers contain. The script below should
simply delete *every* email.

use Mail::pOP3Client;
use warnings;
use strict;

$| = 1;

my($pop, $c, $i);

print "Connecting .... ";
$pop = new Mail::pOP3Client( USER=> "your_username",
PASSWORD=> "your_password",
HOST=> "your_host.com",
AUTH_MODE=> 'PASS',
TIMEOUT => 60,
DEBUG=> 0 );

print "Checking mail .... ";
$c = $pop->Count();
if($c == -1) {print "CONNECTION ERROR\n"}
else {print "$c ", $c == 1 ? 'message' : 'messages', "\n"}

for ($i = 1; $i <= $c; $i++) {$pop->Delete($i)}

$pop->Close() or warn "cannot close: $!";
print "\n$c ", $c == 1 ? 'message' : 'messages', " removed\n";

__END__

Cheers,
Rob
 
F

Fetter

Sisyphus said:
Here's a modified version of a script I use. This modified version is
unchecked - beware of typos and other silly errors.
The actual script I use parses the headers, and decides whether or not to
delete the email based on what the headers contain. The script below should
simply delete *every* email.

use Mail::pOP3Client;
use warnings;
use strict;

$| = 1;

my($pop, $c, $i);

print "Connecting .... ";
$pop = new Mail::pOP3Client( USER=> "your_username",
PASSWORD=> "your_password",
HOST=> "your_host.com",
AUTH_MODE=> 'PASS',
TIMEOUT => 60,
DEBUG=> 0 );

print "Checking mail .... ";
$c = $pop->Count();
if($c == -1) {print "CONNECTION ERROR\n"}
else {print "$c ", $c == 1 ? 'message' : 'messages', "\n"}

for ($i = 1; $i <= $c; $i++) {$pop->Delete($i)}

$pop->Close() or warn "cannot close: $!";
print "\n$c ", $c == 1 ? 'message' : 'messages', " removed\n";

__END__

Cheers,
Rob


Ok i tried this with both gmail.google.com and webmail.aol.com... i get
the connection error every tme. Any ideas?
 
A

A. Sinan Unur

....

Ok i tried this with both gmail.google.com and webmail.aol.com... i
get the connection error every tme. Any ideas?

Yes, check with Gmail and AOL to figure out what settings you need to
use to access your account using POP3 including any TLS settings.

That part is up to you.

Sinan

PS: See http://www.unur.com/comp/ppp/delallspam.html for another
Mail::pOP3Client example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top