Tuesday, June 26, 2007

Automated Emailing of Files

It's not often that things slow down enough that I can catch my breath and record what I have worked on. I had plans of maintaining this Blog once I took my new position at the hospital. But by the time I have documented my work order, I am all ready onto something else.

But today I was able to put some time into a pet project. I had a staff member ask me if we could set up a scanner in the emergency room of the hospital, and any time someone scanned a document it would get emailed to a specific doctor. I figured that there was probably a piece of software for Windows that would do it. And I figured right. But I wasn't looking to shell out $129 - $1,000 of our department budget to get the job done. So I turned to my good friend, Open Source.

Here is a shell script I wrote that will look for the existence of a file, and if found - will ship it off to a lucky recipient by email.

Things to take into account:

  • I created a directory called /mnt/drnobody
  • I mounted //servername/SHARE to /mnt/drnobody/ with mount -t smbfs -o username=myusernamehere,password=mypasswordhere //servername/SHARE /mnt/drnobody/
  • I created a README.txt file which is used by my script to check and see that the share is still accessible

And now, the snippet!

# Dr Example Scan Script - Steve Ballantyne 6.26.06

# Check to see if the share is in place

if [ -f /mnt/drnobody/Sharedir/ER/DrNobodyScan/README.txt ]
# Share exists - so we check to see if a scanned document is waiting
if [ -f /mnt/drnobody/Sharedir/ER/DrNobodyScan/FAX001.pdf ]
# File is waiting so we mail it off
uuencode /mnt/drnobody/Sharedir/ER/DrNobodyScan/FAX001.pdf /tmp/FAX001.pdf | mail -s "ED Document Scan" doctor.nobody@nowhere.com
# REMOVE the file so it doesn't get re-faxed
rm /mnt/drnobody/Sharedir/ER/DrNobodyScan/FAX001.pdf

# No file is waiting so we stop this task and quit
echo "Nothing to be sent right now"
# Uh oh - the share is dead and we panic
echo "Directory doesn't exist, we need a remount - we will try to do it now"
mount -t smbfs -o username=myusernamehere,password=mypasswordhere //servername/SHARE /mnt/drnobody/
/usr/bin/mutt -i panic -s "ED Document Scan FAILED" steve.ballantyne@myemailaddress.com < /dev/null

Now, you may say - "This is crazy! - Who will run this, and how often?". But that is where I slap you with a large bit of logic. I created a cron job (scheduler) that that will run this task every minute, of every hour, of every day. Since this is a scheduled job, we could do without the "echo" stuff too. I just put that there for my own debugging or for when I run this command myself from a terminal. My "crontab" looks like this ...

* * * * * /home/ballantynesd/scriptname.sh > /dev/null

I was able to add it to my crontab with "crontab -e". I had to mark the file executable too, with the usual "chmod 755 scriptname.sh".

Only thing left, is to tell the scanning software to name it's output files as FAX001.pdf, and tell the user's not to scan more than once per minute. ;-) I suppose I could add some support for handling multiple files ... perhaps tomorrorow.

-Steve Ballantyne