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

  5. 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="[email protected]"
    [ "$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="[email protected]"
    [ "$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)

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

Leave a Comment