Remove or Delete all emails message from a POP3 server

last updated in Categories Howto, Linux, Shell scripting, Ubuntu Linux, UNIX

My ISP provided me 5 free email ID, each with 1 GB size. However, one of the POP3 account has been spammed with over 2500+ spam messages. Getting those entire messages will not just waste my time but bandwidth too.

Advertisements

Sample shell script to delete all emails from POP3 server

So here is small shell script I wrote to get rid of all the messages on your POP server.

#!/bin/sh
username="me@myisp.com";
password="mypop3server-password";
MAX_MESS=$1
[ $# -eq 0 ] && exit 1 || :
sleep 2
echo USER $username
sleep 1
echo PASS $password
sleep 2
for (( j = 1 ; j <= $MAX_MESS; j++ ))
do
echo DELE $j
sleep 1
done
echo QUIT

Script usage:

First setup your POP3 username and password. Run this script as follows:
$ ./clean.pop3 2500 | telnet pop3.myisp.com 110
Output:

Trying 61.142.1xx.xxx...
Connected to pop3.myisp.com.akadns.net.
Escape character is '^]'.
+OK hello from popgate(2.34.1)
+OK password required.
+OK maildrop ready, 2501 messages (40690358 octets) (40690358 2147483648)
+OK message 1 marked deleted
+OK message 2 marked deleted
+OK message 3 marked deleted
....

Where,

  • 2500: Total number of POP3 messages to remove
  • telnet pop3.myisp.com 110: Telnet to ISP pop3 server and delete all emails from a POP3 server

If you are on dial-up internet connection this script is handy. If you prefer there is PHP version too .

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

15 comment

  1. I would like to download all my email from a POP3 server into seperate files (like what is normally done I suppose). I just need something short and sweet that will log onto server and download all messages, 1 to a file.

  2. With the STAT command the pop server shows how many msg’s are in the pop-mailbox. The output looks like this:

    +OK 10 74674

    where 10 stands for the numer of mails. How can i modify the script so $MAX_MESS matches the number of messages on the server?

  3. Hi, the original script not found, at least over my ubuntu 6.10

    I do some changes to correct it

    Thanks!
    chcibelli at gmail dot com

    #!/bin/bash

    username=”username”;
    password=”password”;

    MAX_MESS=$1

    [ $# -eq 0 ] && exit 1 || :

    sleep 2

    echo USER $username
    sleep 1
    echo PASS $password

    sleep 1

    for ((i=1;i

  4. I have about 3,800 emails to delete but there are a few genuine messages among them. Can the script be modified to delete a range?

  5. Since this is in bash, the first line should be changed to:
    #!/bin/bash

    This should work for those who are getting errors.

  6. RE_FreSh (I know 6 years too late but I wanted a solution too so here it is)
    output the result of STAT to a file
    parse the stat file
    rerun the code.
    Here’s mine (also needed it to work over pop3s):
    #!/bin/bash
    username=”username”;
    password=”pass”;
    MAX_MESS=1
    (sleep 2
    echo USER $username
    sleep 1
    echo PASS $password
    sleep 2
    echo STAT
    sleep 1
    echo QUIT)| openssl s_client -connect mydomain.com:pop3s -CApath /usr/local/ssl/certs -quiet >test.txt
    lineno=4
    wordno=2
    filename=test.txt
    counter=0
    while read line
    do
    counter=$(( $counter + 1 ))
    if test $counter -eq $lineno
    then
    string=$line
    pattern=”S+”
    count=0
    for word in $string
    do
    [[ $word =~ $pattern ]]
    if [[ ${BASH_REMATCH[0]} ]]
    then
    match=”${match:+match }${BASH_REMATCH[0]}”
    count=$(( $count + 1 ))
    result[$count]=${BASH_REMATCH[0]}
    fi
    done
    mailcount=”${result[$wordno]}”
    fi
    done email1.txt
    fi

  7. I needed a script like this to delete some mails from dummy mailboxes.
    This 6 years old comment helped me a lot. Many thanks.

    Just wanted to share my version of the script. HTH

    I wanted to make it have some defaults, so i can call it without arguments.
    But i also wanted to make it usable for future dummy accounts with arguments

    #!/bin/bash
    # Parsing Arguments
    while [ $# -gt 0 ] ; do
    case "$1" in
            "-u" | "--username" )   username="$2" ;;
            "-p" | "--password" )   password="$2" ;;
            "-h" | "--hostname" )   hostname="$2" ;;
            "-p" | "--port"     )   port="$2"     ;;
    esac
    shift ; shift
    done
    
    # Setting defaults (alternatively you could just exit if these variables are not set)
    [ "$username" == "" ]   && username="foob@r"
    [ "$password" == "" ]   && password="somepass"
    [ "$hostname" == "" ]   && hostname="blackhole.com"
    [ "$port"     == "" ]   && port="pop3s"
    
    # Get number of messages
    messages=`(               sleep 1
    echo USER $username     ; sleep .1
    echo PASS $password     ; sleep .1
    echo stat               ; sleep .1
    echo 'quit'             ; sleep .1 ) | 
    openssl s_client -connect ${hostname}:${port} -CApath /usr/lib/ssl/certs 2>/dev/null | 
    grep OK | tail -2 | head -1 | awk '{ print $2 }'`
    
    # Leave if there is nothing to do
    [ "$messages" -eq 0 ] && exit 0
    
    # Delete Mails
    (                         sleep 2
    echo USER $username     ; sleep .1
    echo PASS $password     ; sleep .1
    for i in `seq 1 $messages` ; do
            echo "dele $i"  ; sleep .1
    done
    echo 'quit'             ; sleep .1 ) | 
    openssl s_client -connect ${hostname}:${port} -CApath /usr/lib/ssl/certs 2>/dev/null 1>&2
    
    # Generate some output
    echo "Deleted $messages messages"
    exit 0
    

    A similar task:
    You can use this one to check for imap mailquota (compatible with nagios or nagios like monitoring systems)
    You can also configure defaults like in the first script.

    
    #!/bin/bash
    # Parsing Arguments
    while [ $# -gt 0 ] ; do
    case "$1" in
            "-u" | "--username" )   username="$2" ;;
            "-p" | "--password" )   password="$2" ;;
            "-h" | "--hostname" )   hostname="$2" ;;
            "-p" | "--port"     )   port="$2"     ;;
            "-w" | "--warning"  )   warning="$2"  ;;
            "-c" | "--critical" )   critical="$2" ;;
    esac
    shift ; shift
    done
    
    # Setting defaults
    [ "$username" == "" ]   && username="foob@r"
    [ "$password" == "" ]   && password="somepass"
    [ "$hostname" == "" ]   && hostname="blackhole.com"
    [ "$port"     == "" ]   && port="993"
    [ "$warning"  == "" ]   && warning=0
    [ "$critical" == "" ]   && critical=0
    
    # Connect and get quota
    storage=`(                                sleep 2
    echo "A LOGIN $username $password"      ; sleep .1
    echo "A GETQUOTAROOT INBOX"             ; sleep .1
    echo "A LOGOUT"                         ; sleep .1 ) | 
    openssl s_client -connect ${hostname}:${port} -CApath /usr/lib/ssl/certs 2>/dev/null | 
    grep STORAGE | cut -d'(' -f2`
    
    quota_max=`echo ${storage%??} | awk '{ print $3 }'`
    quota_use=`echo $storage | awk '{ print $2 }'`
    
    # Calculate Percentages
    quota_percent=$( echo "scale=8 ; $quota_use / $quota_max * 100.0" | bc -q )
    # If less than 1 percent, bc will cut off the first 0, we will put it back there
    [ `echo $quota_percent | grep -ce '^.'` -eq 1 ] && quota_percent="0${quota_percent}"
    
    # Compare with the thresholds (pretty lazy)
    quota_percent_int=$( echo $quota_percent | cut -d'.' -f1 )
    [ "$quota_percent_int" -ge "$warning"  ] && rv=1
    [ "$quota_percent_int" -ge "$critical" ] && rv=2
    
    # Create some output
    echo "$quota_use of $quota_max | ${quota_percent%?????}%"
    exit $rv
    

    (I hope the tags work and it’s displayed readable)

  8. for (()) is a bashism, so you need to change the shebang to #!/bin/bash

    Otherwise, you just saved me 20min riding back home with my back. Thanks!

  9. Hello there,
    I had an issue from Ubuntu. That’s the output:

    vm152:~/software# ./delemail 500 | telnet in.pippo.it 110
    Trying 82.57.200.129…
    Connected to in.pippo.it.
    Escape character is ‘^]’.
    +OK POP3 PROXY server ready
    +OK Password required
    +OK 4168 messages
    ./delemail: 12: ./delemail: Syntax error: Bad for loop variable
    Connection closed by foreign host.
    vm152:~/software#

    Linus Distro is Ubuntu 12.04.4 LTS
    Some ideas?
    TIA

    Have a question? Post it on our forum!