#!/usr/local/bin/bash
MAILFOLDER=$HOME/htmldocs/mailarchive/
MKDIR="mkdir -p"
TMPFILE=`mktemp /tmp/mailiarchXXXXXXXXXX`
LOG=$MAILFOLDER/log
# Procmail postfilter for adding files to a directory structure for flat-file
# databasing of incoming emails sorted by date.
# Tree format will be YYYY/MM/DD/
# File format will be HHMMS-$(subject)
# First, create file
cat > $TMPFILE
# Now, parse out the date
dateline=`grep "^Date: " $TMPFILE | head -n 1`
# Did it find a Date: line?
if [ $? = 0 ]; then
# Place remainder of program here.
# sample date line from my inbox:
# Date: Fri, 2 Jan 2009 14:58:39 -0800
day=`echo $dateline | cut -d\ -f3`
month=`echo $dateline | cut -d\ -f4`
YEAR=`echo $dateline | cut -d\ -f5`
TIME=`echo $dateline | cut -d\ -f6 |sed s/://g`
# Ok, we have the parameters. Lets work out a number for the month.
case $month in
"Jan")
MM=01
;;
"Feb")
MM=02
;;
"Mar")
MM=03
;;
"Apr")
MM=04
;;
"May")
MM=05
;;
"Jun")
MM=06
;;
"Jul")
MM=07
;;
"Aug")
MM=08
;;
"Sep")
MM=09
;;
"Oct")
MM=10
;;
"Nov")
MM=11
;;
"Dec")
MM=12
;;
esac
# Does the day have one digit?
if [ $day -lt 10 ]; then
# add one.
DAY=0$day
else
# day is okay.
DAY=$day
fi
# We now have all the stuff.
# Make a directory and have it fail silently.
mkdir -p $MAILFOLDER/$YEAR/$MM/$DAY 2>/dev/null
# We'll check this one just to ensure the archive works.
cat $TMPFILE > $MAILFOLDER/$YEAR/$MM/$DAY/$TIME 2>/dev/null
if [ $? != 0 ]; then
echo $0: `date` "Failed to record file at "$MAILFOLDER/$YEAR/$MM/$DAY/$TIME"
else
echo $0: `date` "Recorded file at "$MAILFOLDER/$YEAR/$MM/$DAY/$TIME"
fi
else # no date found.
echo $0: `date` "Failed to find date file in email." >> $LOG
fi