{ 13 comments… read them below or add one }

1 Aaron September 23, 2006 at 9:45 am

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.

Reply

2 nixCraft September 23, 2006 at 3:28 pm

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

Reply

3 RE_FreSh October 17, 2006 at 1:22 pm

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?

Reply

4 Christian January 17, 2007 at 3:14 am

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

Reply

5 nixCraft January 17, 2007 at 10:28 pm

Christian,

Can you repost your script using ;<code&gtshell script ;<code> html tags!

Reply

6 rupa August 22, 2007 at 9:10 am

thanks for the script

Reply

7 Ken G February 8, 2008 at 12:18 am

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?

Reply

8 aice l williams June 30, 2010 at 5:45 pm

when i try to send an e-mail it want go thru. pop3 keep popping up

Reply

9 axonmisfire October 27, 2010 at 3:10 pm

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

This should work for those who are getting errors.

Reply

10 Eva Quirinius May 21, 2011 at 10:11 am

With “#!/bin/bash” it works for me. Great. Thanks!

Reply

11 mrpurple January 31, 2012 at 12:45 am

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

Reply

12 sh May 3, 2012 at 7:22 pm

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)

Reply

13 Reini Urban April 20, 2013 at 7:11 pm

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!

Reply

Leave a Comment

Tagged as: , , , , , , ,

Previous post:

Next post: