#!/bin/sh

htmlfile='/var/www/roundcube/reports.html'
directory='/var/www/roundcube/maillog'
FILE='/var/www/roundcube/reports.txt'
DAYS=7
STRING='<reporting@carerighttech.com> -> '

## Generate the Output
rm -f $FILE
for a in `find /var/log -mtime -$DAYS | grep maillog | sort -r`
do
grep -e "$STRING" $a | grep FWD | awk '{ print "Emailed "$12" @ "$1" "$2" "$3}' | awk -F "<" '{ print $1" "$2}' | awk -F ">" '{ print $1$2 }' | awk -F "," '{ print $1$2 }' | awk -F "BODY=7BIT" '{ print $1$2 }' >> $FILE
done

##
## Cleanup dirs older than $DAYS
##

for c in `ls -F $directory/ | grep '/' |sed 's/\///'`
do
time=`expr $DAYS \* 24 \* 60 \* 60`
thentmp=`stat $c --format %z`
then=`date -d "$thentmp" "+%s"`
now=`date "+%s"`
age=`expr $now - $then`
if [ $age -gt $time ]
then
rm -rf $directory/$c
fi
done

##
## Make Directories if they don't exist
## Dump date specific entries from our output into another tmp file
##
while read line
do
ts=`echo $line | awk '{ print $4$5 }'`
mkdir -p $directory/$ts
echo $line >> $directory/$ts/tmpreport.txt
done < $FILE

##
## Generate HTML file
##

for a in `ls -F $directory/ | grep '/' |sed 's/\///'`
do
sort $directory/$a/tmpreport.txt | uniq > $directory/$a/report.txt
done

echo '<html>' > $htmlfile
echo '<title>Dashboard Report Mail Log</title>' >> $htmlfile
echo '<body>' >> $htmlfile
echo '<h2>Dashboard Report Mail Logs</h2>' >> $htmlfile
for a in `ls -F $directory/ | grep '/' |sed 's/\///' | sort -M`
do
b=${a:0:3}
c=${a:3:2}
echo '<a href="/maillog/'$a'/report.txt">Log file for '$b' '$c'</a>' >> $htmlfile
echo '<br/>' >> $htmlfile
done
echo '</body>' >> $htmlfile
echo '</html>' >> $htmlfile

Log file for Jun 3 might look something like this:

Emailed blah0@blah.com @ Jun 3 01:51:34
Emailed blah1@blah.com @ Jun 3 01:14:03
Emailed blah2@blah.com @ Jun 3 03:15:47
Emailed blah3@blah.com @ Jun 3 01:12:11
Emailed blah4@blah.com @ Jun 3 01:11:01