List mails from previous day, per user

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

List mails from previous day, per user

#! /bin/bash

# requires smtpd_log_access_permit_actions = static:all in /etc/postfix/
# will not list emails sent to multiple users (not an issue for me)

if [ "$#" -eq 0 ];then
 echo “include pattern for user”
 TITLE=`gdate --date='yesterday' +'%d %B %Y'`
 echo "<!DOCTYPE html><html><head><title>$TITLE \"$1\"</title> <style>"
   echo 'table { border-collapse: collapse; }
   td,th { border-top: 1px solid black; border-bottom: 1px solid black; Padding: .5em; }
   tr { border-left: 1px black solid; border-right: 1px black solid; }
     <th style="width:10em;">QUEUEID</th><th style="width:6em;">Time</th><th>From</th>

  bzgrep -E 'DATA|\"from=\"' /var/log/maillog.0.bz2 | grep -v "<>"| \
    awk '{print $6"\t"$3"\t"$17"\t"$16}' | grep -v ESMTP | \
    grep -v "to=<backup" | column -t | sort -k 2 | grep "to=<$*" | \
    sed 's/from=<//g' | sed 's/>//g' |
    awk '{print "<tr><td>"$1"</td><td>"$2"</td><td>"$4"</td></tr>"}'

 echo "</table></body></html>"

And then for the user who want a daily report, I have the following in crontab

DAY=gdate +%d-%b-%Y
2 0 * * * /usr/local/bin/maildayHTML kremels | mutt -e 'set content_type=text/html' -s "DMR List $($DAY)” [hidden email]

(this obviously requires that gnu date and mutt be installed, but I believe this is also possible with straight sendmail, though more complicated)

I don’t know if any mail clients use the <title> from the HTML, but I figured I’d include it just in case.

I have a plain text version also, but it is far less readable, so everyone who’s been given the option has chosen the HTML. Just take out the echo

The email looks like this on


Clicked "Debug" button. Program still has bugs. Wtf? - Rich Seigel