Linux sendmail Primer Tutorial

Linux sendmail Primer Tutorial

Linux sendmail Primer Tutorial

The Linux mailx Primer Tutorial outlined some very useful Linux (and Unix and perhaps macOS (or Mac OS X)) command line tools to email useful information to emailees.

Today, we want to outline a sendmail alternative approach for reporting purposes, perhaps, again, from the command line, and we’d like to thank this link for great advice, here.

Along the way, today, towards the creation of a Korn Shell Script solution to a reporting issue where we wanted to monitor a Linux system’s “ps -ef” mentions of “/world.php” (for our rjmprogramming.com.au CentOS Linux web server operating system), and email relevant report information as a result of that monitoring environment changing, we wanted to emphasise …

  • usefulness of Linux echo‘s -e switch …

    -e enable interpretation of backslash escapes

    … to help out the construction of sendmail headers that require line feed characters to work

  • usefulness of Linux/Unix backtick to include varieties of data into the subject and body sections of the email
  • usefulness of sendmail headers featuring heavily in the Korn Shell Email (from command line) commands as per

    echo -e "Content-Type: text/plain\nFrom: rmetcalfe@rjmprogramming.com.au\nto: rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: First Date ps-ef world.php Report at `date`\n\n`cat date_ps_ef_first.txt`" | sendmail -t -f rmetcalfe15@gmail.com
    echo -e "Content-Type: text/plain\nFrom: rmetcalfe@rjmprogramming.com.au\nto: rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: Changed Date ps-ef world.php Report at `date`\n\n`diff date_ps_ef_first.txt date_ps_ef_next.txt`" | sendmail -t -f rmetcalfe15@gmail.com
  • usefulness of & background processing as per the …

    ksh date_ps_ef.ksh > /dev/null 2> /dev/null &

    … we ended up setting off our date_ps_ef.ksh Korn Shell Script reporter …

    #!/bin/ksh
    # date_ps_ef.ksh
    # Report on world.php
    lastcur=""
    while [ 0 -lt 1 ]; do
    cur="`ps -ef | grep '/world.php' | grep -v 'grep ' | wc -l`"
    echo "`date` $cur"
    if [ "$lastcur" != "$cur" ]; then
    if [ -z "$lastcur" ]; then
    echo "`ps -ef | grep '/world.php' | grep -v 'grep '`" > date_ps_ef_first.txt
    echo -e "Content-Type: text/plain\nFrom: rmetcalfe@rjmprogramming.com.au\nto: rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: First Date ps-ef world.php Report at `date`\n\n`cat date_ps_ef_first.txt`" | sendmail -t -f rmetcalfe15@gmail.com
    else
    echo "`ps -ef | grep '/world.php' | grep -v 'grep '`" > date_ps_ef_next.txt
    echo -e "Content-Type: text/plain\nFrom: rmetcalfe@rjmprogramming.com.au\nto: rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: Changed Date ps-ef world.php Report at `date`\n\n`diff date_ps_ef_first.txt date_ps_ef_next.txt`" | sendmail -t -f rmetcalfe15@gmail.com
    cat date_ps_ef_next.txt > date_ps_ef_first.txt
    fi
    lastcur="$cur"
    fi
    # ps -ef | grep '/world.php' | grep -v 'grep '
    sleep 30
    done
    exit

Take a look at some of all this in action with today’s PDF presentation.


Previous relevant Linux mailx Primer Tutorial is shown below.

Linux mailx Primer Tutorial

Linux mailx Primer Tutorial

Think Linux and you may not associate it with email, but Linux can be excellent for many email requirements, especially using mailx and uuencode. The use of uuencode allows you to give your email an attachment, and the mailx -s switch for the subject of the email can be used for both the purpose it was meant for, that is to establish an email subject, and with a stretch of imagination of usage, the body text, because of the quirk whereby anything after the first line of the subject will spill over into the body text of the email. So, am sure there would be limits to the length of this body text, but you can often be brief when there is a good attachment to provide your email recipient. Naturally, in the day and age of worry about computer viruses, your attachment will most likely be vetted by the receiving email client software for viability, so be aware of this. Other than all that, this method of sending emails is potentially very powerful. Notice that great way Linux and Unix can use `cat body.txt` type of syntax to embed one command within another … sometimes Linux is so simple, powerful, brilliant! You may have noticed how short all the commands in Linux and Unix tend to be, and that is because it was intended to be short and powerful, and this is brilliant.

Background reading for tutorial:

Here is a tutorial that introduces you to some email with attachment, subject and body text from the Linux command line.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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