Mail collection - Automatic mail script




The fetchmail and sendmail commands need to be put together into a script which can be run automatically.

This is the script that I use; I have put it in /usr/local/etc/poll.mail:

#! /bin/bash
# Poll for mail
# May be run either by cron or at will by the superuser.

PATH=/bin:/usr/bin:/etc:/sbin:/usr/sbin:/usr/local/bin

# See if we already have PPP running
if netstat -nr | grep ppp0 >/dev/null
then
	echo PPP already running
	ppp_running=TRUE
else
#	if not, start it up...
	if [ -f /var/run/diald.pid ] && ps -p `cat /var/run/diald.pid`
	then
		echo diald is running
		ping -c 1 mail.enterprise.net
		ppp_running=TRUE
	else
		ppp_running=FALSE
		ppp-on
	fi
	sleep 2

	waitpd=0
	until netstat -nr | grep ppp0 >/dev/null
	do
		if ps -axu | grep -v grep | grep -E 'pppd|diald' >/dev/null
		then
			sleep 5
			waitpd=`expr $waitpd + 5`
			if [ $waitpd -gt 120 ]
			then
				echo Timed out
				exit 2
			fi
		else
			echo PPP session was not established
			exit 1
		fi
	done	
fi
sleep 5

# Collect any mail that is waiting for us
echo "Calling for mail for lfix.co.uk"
fetchmail -aF -f /root/.fetchmail1rc
case $? in	0) :;;			 # no problem
		1) echo No mail to collect;;
		2) echo Could not open socket;;
		3) echo User authentication failed;;
		4) echo Fatal protocol error;;
		5) echo syntax error in fetchmail command;;
		6) echo Bad permissions for run control file;;
		7) echo Server error reported;;
		8) echo Exclusion error;;
		9) echo SMTP failure;;
		10) echo Undefined error \(bug in fetchmail\);;
		*) echo Totally unexpected error in fetchmail;;
esac

echo "Calling for mail for Oliver.Elphick@lfix.co.uk"
fetchmail -aF
case $? in	0) :;;			 # no problem
		1) echo No mail to collect;;
		2) echo Could not open socket;;
		3) echo User authentication failed;;
		4) echo Fatal protocol error;;
		5) echo syntax error in fetchmail command;;
		6) echo Bad permissions for run control file;;
		7) echo Server error reported;;
		8) echo Exclusion error;;
		9) echo SMTP failure;;
		10) echo Undefined error \(bug in fetchmail\);;
		*) echo Totally unexpected error in fetchmail;;
esac

# Send anything we have for the outside world
sendmail -q

# Get news
/usr/local/bin/slurp news.enterprise.net

# If we started PPP, stop it again
if [ "$ppp_running" = FALSE ]
then
	ppp-off
fi
This is what the script does:
  1. It uses netstat -nr to see whether there is a PPP connection running. If you use SLIP, you should change ppp0 to sl0, both here and a few lines down where the same command is used again.
  2. If there is no running connection, the script checks to see if diald is running. If it is, it pings the mail server once, in order to get diald to start up a connection. If diald is not running, the script runs ppp-on to start PPP. It loops until netstat -nr reports a ppp0 connection. If, in the meantime, there is neither a pppd process nor a diald process, the script decides that pppd has failed to connect and terminates itself with return code 1. If it takes more than 2 minutes to establish a connection, the script times out with return code 2.
  3. Once the PPP connection is established, the script runs fetchmail. If there is any mail to collect, the return code is 0; if there is no mail, the return code is 1; if there is some error, the return code indicates what it is and the script prints an appropriate message.
  4. Next, sendmail -q is run in order to send out anything that may be waiting to be sent from our system, and then slurp is run to download news for the local news server.
  5. Finally, if we had to start PPP, we stop it again with ppp-off. If we didn't start it, we leave it alone, because we assume that someone is running an interactive session. If we are running diald, we also leave it alone, because diald will drop the line when the last logical connection is closed.

Once you have created the script, make it executable by the command

# chmod 744 /usr/local/etc/poll.mail
and then set up a crontab entry to run it automatically.


Up to the Automatic mail collection and delivery page

Last updated on 5th May 1997 by Oliver Elphick