At this point, most people are aware of the "fortune | cowsay" command(s) (and if you haven't tried it yet, do it). What I wanted to do, was to have the output from this command emailed out. Simple, right? Not as much as you'd think...

It sounds simple enough, take the command output, and email it out. But we can't have this TOO simple now, can we? When running this command, you will only get the 1 cow "character", when in fact, there are multiples; so since the fortune is random, I'd like the character to be random as well.

So, to start things off, there are a few prerequisits that need to be fulfilled prior to this working:

  1. The mutt email client must be installed and configured.
    • See my previous article on the subject
  2. The 'fortune' and 'cowsay' binaries
    • these can be installed via command:
      • apt-get install fortune cowsay
  3. Something else I thought... But I could be wrong too.

In the end, there is no simple command that would do this for me, so I wrote a little script that does this all. You can download it here:

Cannot find the download. ID=12

Below, we'll go over the good details. I won't go over the details in the same order as they are in the script, rather, I will go over them as I "developped" and figured this out.

First, we need to list all the cowsay cows, and randomly pick one:

# List all cowsay options
Options=$(cowsay -l)

# Remove the first line
PRE="Cow files in /usr/share/cowsay/cows: "
START=${#PRE}

# Put all the options into a list
Options2=${Options:START}
SPACE=' '; set -f
eval "LIST=(\$Options2)"

# Randomly pick one of the cowsay options
num_LIST=${#LIST[*]}
FINAL_OPTION=${LIST[$((RANDOM%num_LIST))]}

Next, the "meat" of the script, generating the "fortune":

FORTUNE=$(fortune | cowsay -f $FINAL_OPTION)

Now, let us email it!

echo "$FORTUNE" | mutt -s "Your Fortune!" This email address is being protected from spambots. You need JavaScript enabled to view it.

But what is this? The formatting is all off!? What is going on? Simple really... In your console session, the formatting is fine, and that is because of the monospacing of the fonts on the console. What this means, is that the letter 'i' visually takes up as much space as the letter 'W', unlike the font here. So how can we do that? At first, I thought of using some basic Rich Text Formatting (RTF), but there isn't any really effective to 100% make SURE that the recepient has the same font to properly display the fortune. In the end, I decided to format the email itself as an HTML email, wrap the fortune around the appropriate HTML tags, and have that take care of the font. Let's get that going.

First, we'll need the starting and ending HTML tags:

HTML_START="<!DOCTYPE html><html><body><pre style=font-family:\"Courier New\">"
HTML_END="</pre></body></html>"

Next, we'll put the HTML email togather:

FINAL_FORTUNE=$HTML_START
FINAL_FORTUNE+="$FORTUNE"
FINAL_FORTUNE+=$HTML_END

Next, we will also allow for multiple email addresses:

# Email addresses in format:
#    This email address is being protected from spambots. You need JavaScript enabled to view it.,This email address is being protected from spambots. You need JavaScript enabled to view it.,This email address is being protected from spambots. You need JavaScript enabled to view it.
EMAILS="This email address is being protected from spambots. You need JavaScript enabled to view it."

And finally, send the email, formatted to HTML:

echo "$FINAL_FORTUNE" | mutt -e "set content_type=text/html" -s "Your Fortune!" $EMAILS

And done! The email is now properly formatted!

The last thing I wanted to do here, was to set a scheduled cron job to run the script and send me a fortune every morning at 10am. Let us edit the cron jobs:

crontab -e

and add the following line:

0 10 * * * /location/of/script.sh

Now, every 10am the script will run and email will be sent out.

Go ahead and let me know what you think of the script below. Any and all comments, suggestions, and/or improvements are welcome!