Professional Web Applications Themes

Script to delete email from an account - PERL Modules

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. # ...

  1. #1

    Default Script to delete email from an account

    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='infoanyaddress.usa'; # enter your
    pop3 username
    $pass='X'; # 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;

    Terry Guest

  2. #2

    Default Re: Script to delete email from an account

    I just realized I posted this in Perl Modules. I meant to post this in
    Perl Misc. I'm going to cross post it in Perl Misc.

    Sorry for the inconvience.

    Terry Guest

  3. #3

    Default Re: Script to delete email from an account

    "Terry" <thawkinstcbnetworks.net> wrote:
    > I just realized I posted this in Perl Modules. I meant to post this in
    > Perl Misc. I'm going to cross post it in Perl Misc.
    You mean multipost (which is often a bad thing), since you can't crosspost
    the message after it has been posted (ok, you can quote it and add
    groups).

    --
    John Small Perl scripts: [url]http://johnbokma.com/perl/[/url]
    Perl programmer available: [url]http://castleamber.com/[/url]
    I ploink googlegroups.com :-)

    John Bokma Guest

Similar Threads

  1. Connenting email form to an email account
    By evaughn in forum Macromedia Dynamic HTML
    Replies: 3
    Last Post: February 15th, 04:00 AM
  2. Unix Account not receiving email
    By Mark C in forum Linux / Unix Administration
    Replies: 2
    Last Post: November 27th, 11:33 PM
  3. Account activation via email
    By R. Rajesh Jeba Anbiah in forum PHP Development
    Replies: 2
    Last Post: October 28th, 04:45 AM
  4. email account and other passwords not saved
    By Guy R. McLean in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 16th, 10:47 PM
  5. delete user account
    By Brian in forum Windows Setup, Administration & Security
    Replies: 0
    Last Post: July 13th, 05:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139