Deleting all mails with a simple Perl script

I have a few alternative mail accounts, mostly used when I need to register with a site I don't trust and thus when I'm expecting spam. Checking last night, I noticed that 3 accounts were filled up with a grand total of 3500 spams, so I wrote a small Perl script, using Net::POP3, to delete all mails in an account.

  1. #!/usr/bin/perl
  2. use Net::POP3;
  3. if (@ARGV < 2) {
  4.     print "Usage : $0 <login> <pass>\n";
  5.     exit 0;
  6. }
  7.  
  8. print "Going to delete ALL mail from account $ARGV[0] ! ctrl-c to abort\n";
  9.  
  10. sleep 3;
  11. $pop = Net::POP3->new('pop.free.fr');
  12. die "Couldn't log on to server" unless $pop;
  13.  
  14. $total = $pop->login($ARGV[0],$ARGV[1]);
  15. die "No mails or couldn't connect !" unless $total;
  16.  
  17. print("Deleting $total mails ...\n");
  18.  
  19. for($i = 0; $i<$tot; $i++) {
  20.     $pop->delete($i);
  21. }
  22.  
  23. $pop->quit();
  24.  
  25. exit 1;

5 comments

  1. dju`

    cool. perhaps adding the POP server as a third argument would be a good idea…

  2. ralf

    Nice script, but has a little error:
    for($i = 0; $i

  3. Ozh

    Hmm, what is the error then ? Works fine for me :)

  4. Brian

    The error is that the for loop variable is "$tot" where it should be "$total"

  5. Mike

    Hi I had an error with this script. If you only have 1 Mail and you will begin ($i = 0; $i<$tot; $i++) my msg index starts with 1 and not with 0 so no emails will deleted because there is no MSG with the index 0.

Comments are closed.