Monday, December 24, 2007

Cronic Emailing

So now Comcast knows we're leaving. I'm assuming that this means that they will eventually shut off my old Comcast email address. I could check this by logging onto my old Comcast account every day, but I decided it would be simpler to let the computer do it: Comcast currently forwards email to my Verizon account, so let's send my Comcast account an email. Either I'll get it, via Verizon, or it will be bounced, in which case I'll get an email from Comcast telling me to buzz off. I'd like this to be done daily, say at 1 am. Then when I check my email in the morning I know immediately if Comcast is still forwarding mail.

To do this, we need several utilities. First, we need a command to allow the computer to send mail, preferably from the command line. In Unix-like systems, this is called mail (Duh). Well, that's not quite right. First we need a mail server. We'll use Gmail's SMTP server, located at (Who thinks up these names?) Second, we need a program to access Gmail's SMTP. That would be msmtp, which we've discussed before. Finally, we need a command line program mailer. That would be the aforementioned mail, except that it's not the standard one that's loaded in with Ubuntu. Ubuntu comes with mail from the GNU mailutils package. That doesn't work well with msmtp. Instead, you want the simpler mailx package. So fire up a command-line prompt and type:

$ sudo apt-get install mailx

adding msmtp to that line if you don't have it installed. This will remove mailutils and install mailx.

Now that all our tools are in place, we just need to set things up. First, let's work on msmtp. We need to create, or edit, the file $HOME/.msmtprc. The contents have changed just a bit from the original install. Here, with some obvious changes, is my current file:

account default
auth on
password nuculer
tls on
port 587
tls_certcheck off

port 587 is Google's preferred email port, and tls_certcheck off tells the program to not bother about doing certificate checks — which means that you should probably not use this method to send anything of value. Also note that your password is in clear, so you'd better set the permissions of $HOME/.mtmsprc to keep anyone else from reading it:

$ chmod 600 $HOME/.mtmsprc

Now, let's set up the message. I want a script, basically, that sends a short message to my Comcast account. So I set up this little bit of code:

$ cat > $HOME/cmailtest
mail -s "Comcast Mail Forwarding Check" << endmail
Today, it's still working.
$ chmod 700 $HOME/cmailtest

Which creates a file, called cmailtest, in my home directory, that sends a simple email. The file is made readable and executable by me and me alone, just in case someone else stumbles upon it and starts sending it to me at random times.

Send the email by executing the file from the command line:

$ $HOME/cmailtest

That's fine, but how do I get it sent at 1 am every day?

That requires the cron and crontab commands. cron is a daemon that searches for things to do. Most of these are specified in the /etc/cron* directories, but you can add things to the list as well. In my case, we do this:

$ cat > cfile
0 1 * * * /home/dubya/cmailtest

$ crontab cfile

This creates a file, cfile, which will tell cron to run the cmailtest script at the 0th minute of the first hour of every (the “*”) day, week, and month. The blank line is to ensure that there is an end-of-line after cmailtest, otherwise cron won't recognize it. The command is entered into the system with the crontab command.

And that's it. From now on, Hal will send a message to my old Comcast account early in each day. As long as the account exists, I'll get it at my Verizon email. Once the account dies, Gmail will get a bounce message.

So when that happens how do I turn the thing off? Simple. I just use the

$ crontab -r

to delete all of my cron jobs. Or, if I want to add other things to the list, edit the cron jobs with

$ crontab -e

For more examples, see the previously mentioned link, a more Ubuntu-specific page or, of course, try

$ man cron
$ man crontab


rcjhawk said...

And late last night it occurred to me that you could make the mail command a one-liner, and add more information as well:

echo "Email still working on " `date` | mail -s "Comcast Mail Forwarding Check"