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.
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 .
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 13 comments… read them below or add one }
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.
Use Fetchmail to get email from POP3 mail to local Linux / UNIX a/c see url for more info:
http://theos.in/howto-configure-fetchmail-linux-or-unix-client-program-to-fetch-emails.html
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?
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
Christian,
Can you repost your script using ;<code>shell script ;<code> html tags!
thanks for the script
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?
when i try to send an e-mail it want go thru. pop3 keep popping up
Since this is in bash, the first line should be changed to:
#!/bin/bash
This should work for those who are getting errors.
With “#!/bin/bash” it works for me. Great. Thanks!
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
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 0A 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)
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!