unix :: scan and email a ‘sent mail’ log

I have a few web servers…just a few. One of my web servers provides a reminder system in which it scans a classified every few minutes, and emails subscribers when the website changes and matches a users search criteria. It’s handy, but sometimes I wonder if people are getting their emails. To combat this, I added myself as a subscriber and I receive emails every day, however I still wanted to know how many of my subscribers are getting their emails. The only way was to troll through the mail log and check it.If you’ve ever seen a unix mail log, its messy, and ugly. Sure, you can get your information out, but its annoying. I’m not bad with grep and can whip up a quick statement to reformat the output of the log, but I wanted something better, so I wrote a little script.

What the script does, is scan the mail.log file every day, clean it up from all the junk that I don’t want to see (such as mail in, bounced mail etc) and just send myself an email of who my web server sent an email to.
You will need to modify it slightly to meet your criteria, but here it is. I’ve provided as much comments as possible, if anything is vague, leave a comment and I’ll reply instantly (if I’m awake!).

#bash script to email me sent emails per day
#remove old maillog files, this will fail the first time 
rm /var/log/tempmail.log
rm /var/log/tempmailSent.log
#first, copy the contents of the mail log to a temp file, not required, you can do all this inline, but i like to have something to debug if something goes wrong
grep -Fv "root" /var/log/mail.log >> /var/log/tempmail.log 
#now strip the file and only keep lines with the word 'sent' in it, again, to a separate file
grep -F "sent" /var/log/tempmail.log >> /var/log/tempmailSent.log
#This removes all text between two words, in this instance I delete any data between the world 'mishmashmoo' and 'to='
sed -i 's/mishmashmoo.*to=//g' /var/log/tempmailSent.log 
#and this removes text between ',' and ')' which cleans up the message. I then save it all to tempmailSent.log
sed -i 's/,.*)//g' /var/log/tempmailSent.log
#now my tempmailSent.log has only the data I want, so I just send it (commands will differ based on your email system)
mail -aFrom:emailWatcher@mishmashmoo.com -s "Sent Emails Today" me@email.com < /var/log/tempmailSent.log

This works really well, and I just setup a crontab to run every day at a particular time. I glance at it, and it makes me happy to know my users are getting emails.

The file looks like this

Nov 10 07:00:05 <user1@hotmail.com>
Nov 10 07:00:05 <suser2@gmail.com>
Nov 10 07:15:04 <user3@gmail.com>
Nov 10 11:00:06 <user2@hotmail.com>
Nov 10 11:00:07 <user3@gmail.com>
Nov 10 11:00:11 <user5@nrpw.com.au>
Nov 10 11:14:51 <user34@gmail.com>

which looks much nicer than a mail log.


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>