Linux sendmail Mime Type Attachment Tutorial

Linux sendmail Mime Type Attachment Tutorial

Linux sendmail Mime Type Attachment Tutorial

Yesterday’s Linux sendmail Content Type Tutorial had us …

  • following up (the Primer tutorial) sending of Content-Type: text/plain emails via sendmail on Linux … by …
  • allowing for Inline HTML Email (report) sending of Content-Type: text/html emails via sendmail on Linux … but …
  • there are more Mime Types than just these two, but we can make email attachments out of other Mime Type data forms (often regarding “image”, “video” and “audio” media Mime Types)

… and, of course, this opens up this command line Korn Shell scripting to so much more about what the Internet is about “sharing media files” (often created on mobile devices by the users themselves). This ability makes users “uploaders” of content, as so many more of us are becoming every day.

We have a favoured approach to interpretive software (though the same approach works well with non-interpretive coding too) language (the types that do not need compiling) development where a project …

  • starts as a guinea pig exercise to solve a specific set of criteria (remember “Checking on /world.php ps -ef Reporting”?) … and we …
  • genericize by “parameterizing” the “hardcoded words” in the code … for example, the color coded “hardcodings” of yesterday’s Linux sendmail Content Type Tutorial “First Report” email creation …

    echo -e "Content-Type: text/html\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<html><head><style> tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} </style></head><body><h1>First Date ps-ef world.php Report at `date`</h1><table style='border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'><tr><th>UID</th><th>PID</th><th>PPID</th><th>C</th><th>STIME</th><th>TTY</th><th>TIME</th><th>CMD</th></tr>`cat date_ps_ef_first.txt | sed '/^/s//<tr><td>/g' | sed 's/ \{1,\}/<\/td><td>/g' | awk '{ t=0; while (getline \$0) { if (t == 0) { print \"<tr style=background-color:#e7e7e7 !important;\" substr(\$0,3); } else { print \$0; } t=1-t; } }' | sed '/$/s//<\/td><\/tr>/g'`</table></body></html>" | sendmail -t -f rmetcalfe15@gmail.com

    … becomes …

    echo -e "${mimetype}From: ${emailfrom}\nTo: ${emailto}\nCC: ${emailcc}\n${emailbcc}Subject: First ${subjmiddle} at `date`\n${attachrest}" | sendmail -t -f ${emailto}

    … via …
  • command line argument logic (ie. parameterization) helping derive those Korn Shell Script “$” variable value determination …
    #!/bin/ksh

    mimetype="Content-Type: text/html\\n"
    emailfrom="rmetcalfe@rjmprogramming.com.au"
    emailto="rmetcalfe15@gmail.com"
    emailcc="rmetcalfe@rjmprogramming.com.au"
    emailbcc=""
    subjmiddle="Date ps-ef world.php Report"
    lastcur=""
    filen=""

    attachrest="\\n<html><head><style> tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} </style></head><body><h1>First First ${subjmiddle} at `date`</h1><table style='border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'><tr><th>UID</th><th>PID</th><th>PPID</th><th>C</th><th>STIME</th><th>TTY</th><th>TIME</th><th>CMD</th></tr>`cat date_ps_ef_first.txt | sed '/^/s//<tr><td>/g' | sed 's/ \{1,\}/<\/td><td>/g' | awk '{ t=0; while (getline \$0) { if (t == 0) { print \"<tr style=background-color:#e7e7e7 !important;\" substr(\$0,3); } else { print \$0; } t=1-t; } }' | sed '/$/s//<\/td><\/tr>/g'`</table></body></html>"
    if [ ! -z "$1" ]; then
    while [ ! -z "$1" ]; do
    if [ "`echo \"${1}\" | tr -d '@'`" != "`echo \"${1}\"" ]; then
    if [ "`echo \"${emailto}\" | tr -d ' '`" == "`echo \"${emailto}\"" ]; then
    emailto="${1} "
    elif [ "`echo \"${emailcc}\" | tr -d ' '`" == "`echo \"${emailcc}\"" ]; then
    emailcc="${1} "
    else
    emailbcc="BCC: ${1}\\n"
    fi
    elif [ -f "${1}" ]; then
    if [ ! -z "${mimetype}" ]; then
    filen="${1}"
    mimetype=""
    shortf=""
    basechars="`base64 ${filen} | tr -d \"\\n\"`"
    i=0
    flast="`echo ${1} | tr '/' ' ' | sed '/\\\\/s// /g'`"
    for filebits in ${flast}; do
    shortf=$filebits
    ((i=i+1))
    done
    subjmiddle="$shortf"
    attachrest="Mime-Version: 1.0\\nContent-Type: multipart/mixed; boundary=\"19032019ABCDE\"\\n\\n--19032019ABCDE\\nContent-Type: text/plain; charset=\"iso-8859-1\"\\nContent-Transfer-Encoding: 8bit\\n\\nFirst ${subjmiddle} at `date`\\n\\n--19032019ABCDE\\nContent-Type: application/octet-stream; name=\"${shortf}\";\\nContent-Transfer-Encoding: base64\\nContent-Disposition: attachment;filename=\"${shortf}\"\\n\\n${basechars}\\n--19032019ABCDE--"
    fi
    fi
    shift
    done
    emailfrom="`echo ${emailfrom} | tr -d ' '`"
    emailto="`echo ${emailto} | tr -d ' '`"
    emailcc="`echo ${emailcc} | tr -d ' '`"
    fi

    … thanks to this great webpage here for great assistance …the main extra argument can point at a rjmprogramming.com.au domain web server file

… leaving …


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

… unchanged as a rjmprogramming.com.au domain “/world.php” ps -ef Report monitor … and the same code called …


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

… serving to email a “First Report” email attachment that is that “please_sign.mp4″ video (media) file (playable for the emailee at that email), and if anything changes about that file a “Changed Report” email attachment that is that “please_sign.mp4″ video (media) file (playable for the emailee at that email) will be sent to the same emailee recipients.

And so the changed date_ps_ef.ksh Korn Shell script has a far bigger “potential footprint” as of this incarnation.


Previous relevant Linux sendmail Content Type Tutorial is shown below.

Linux sendmail Content Type Tutorial

Linux sendmail Content Type Tutorial

Yesterday’s Linux sendmail Primer Tutorial‘s command line emailing really felt “command line”. We do not consider this an insult per se, and we tend towards “the badge of honour thinking” here. But given reports are more trusted when impactive (we dare to figure) … so … let’s add a bit of “spark” … luckily the CSS restrictions of HTML Inline Email “curbs the enthusiasm” anyway … to the email reporting started yesterday.

The key to what we change is …

  • in the sendmail headers we change Content-Type: text/plain to Content-Type: text/html and that step alone opens the door to “a little bit of” style in the body section of the email … but …
  • given a lot of CSS3 is not supported in HTML Inline Email styling we had to forgo …

    style="background:linear-gradient(top,pink,yellow);"

    … for a linear-gradient background … and …

    <head><style> tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} </style</head>

    … for alternate table row colouring (as per the heads up at this link, thanks) … for good ‘ol …

    1. sed (thanks to this useful link for advice)
    2. awk (and its excellent getline method, and thanks to this useful link for advice)

    … scripting interventions instead … so that for that “First Report” we output the report (largely) in an HTML table element as per …

    echo -e "Content-Type: text/html\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<html><head><style> tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} </style></head><body><h1>First Date ps-ef world.php Report at `date`</h1><table style='border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'><tr><th>UID</th><th>PID</th><th>PPID</th><th>C</th><th>STIME</th><th>TTY</th><th>TIME</th><th>CMD</th></tr>`cat date_ps_ef_first.txt | sed '/^/s//<tr><td>/g' | sed 's/ \{1,\}/<\/td><td>/g' | awk '{ t=0; while (getline \$0) { if (t == 0) { print \"<tr style=background-color:#e7e7e7 !important;\" substr(\$0,3); } else { print \$0; } t=1-t; } }' | sed '/$/s//<\/td><\/tr>/g'`</table></body></html>" | sendmail -t -f rmetcalfe15@gmail.com

    … and for the “Changed Report” we output the report (largely) in an HTML textarea element as per …

    echo -e "Content-Type: text/html\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<html><body><h1>Changed Date ps-ef world.php Report at `date`</h1><textarea rows=${cur} style='background-color:#e7e7e7;border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'>`diff date_ps_ef_first.txt date_ps_ef_next.txt`</textarea></body></html>" | sendmail -t -f rmetcalfe15@gmail.com

To get to the HTML textarea element stage we made these changes, and then to progress onto a tabular “First Report” we applied these changes to the final date_ps_ef.ksh Korn Shell script that finished up on the rjmprogramming.com.au web server, again, started via …


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

… to “do its thaing” in the background, quietly, like.


Previous relevant Linux sendmail Primer Tutorial is shown below.

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.


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, Software, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>