Getting mail via IMAP protocol using Curl and bash

06/06/2016 By emehany

#This is a work in prgress - the goal of this script is to parse mail box names and grap the first 10 or so emails in 
each folder.


base=/base/path/mail
outputfile=$base'/data.txt'
temp=$base'/temp.txt'
folderNames=$base'/folders.txt'
folderContent=$base'/content.txt'

#
# Retrieve all folders names
#
curl --url "imap.server.com" --user "user@example.com:test" > $outputfile

#
# Empty files that will hold the below curl calls results
#
echo '' > $folderNames
echo '' > $temp
echo '' > $folderContent

while read line
do
line=${line%$'\r'}
echo $line | sed 's/.*"\/" //' >> $temp
done < $outputfile

while read line
do
line=${line%$'\r'}
echo $line | sed 's/"//' | sed 's/"//' | sed 's/ /%20/' | sed 's/ //' >> $folderNames
done < $temp

while read line
do
if [ ! -z "$line" -a "$line" != "" ]; then
echo "Saveing "$line 1>&3
url="imap.server.com/$line"
url=${url%$'\r'}
folderName="$base/$line.txt"
curl --url $url --user "user@example.com:test" > $folderName
fi
done < $folderNames

# Examine a folder - returns the total number of messages etc the append the results in each file

while read line
do
if [ ! -z "$line" -a "$line" != "" ]; then
echo "Saveing "$line 1>&3
url="imap.secureserver.net/$line"
url=${url%$'\r'}
folderName="$base/$line.txt"
#curl --url $url --user "user@example.com:test" > $folderName
curl --url $url --user "user@example.com:test" --request "EXAMINE $line" >> $folderName
fi
done < $folderNames

 

 

 

reference:

https://debian-administration.org/article/726/Performing_IMAP_queries_via_curl

 

Leave a comment

Login to Comment

Loading