{"id":49016,"date":"2020-05-16T03:01:50","date_gmt":"2020-05-15T17:01:50","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=49016"},"modified":"2020-05-16T07:21:48","modified_gmt":"2020-05-15T21:21:48","slug":"linux-sendmail-content-type-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-sendmail-content-type-tutorial\/","title":{"rendered":"Linux sendmail Content Type Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/sendmail_command_line_content_type.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux sendmail Content Type Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/sendmail_command_line_content_type.jpg\" title=\"Linux sendmail Content Type Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux sendmail Content Type Tutorial<\/p><\/div>\n<p>Yesterday&#8217;s <a title='Linux sendmail Primer Tutorial' href='#lpt'>Linux sendmail Primer Tutorial<\/a>&#8216;s command line emailing really felt &#8220;command line&#8221;.  We do not consider this an insult per se, and we tend towards &#8220;the badge of honour thinking&#8221; here.  But given reports are more trusted when impactive <font size=1>(we dare to figure)<\/font> &#8230; so &#8230; let&#8217;s add a <i>bit of<\/i> &#8220;spark&#8221; <font size=1>&#8230; luckily the CSS restrictions of HTML Inline Email <a target=_blank title='?' href='https:\/\/www.youtube.com\/watch?v=m7BgOYPNTlA'>&#8220;curbs the enthusiasm&#8221;<\/a> anyway &#8230;<\/font> to the email reporting started yesterday.<\/p>\n<p>The key to what we change is &#8230;<\/p>\n<ul>\n<li>in the <a target=_blank title='Linux sendmail' href='https:\/\/linux.die.net\/man\/8\/sendmail.sendmail'>sendmail<\/a> <a target=_blank title='Linux sendmail headers' href='https:\/\/serverfault.com\/questions\/347602\/sending-e-mail-from-sendmail-with-headers'>headers<\/a> we change <i><a target=_blank title='Content-Type Google search' href='https:\/\/www.google.com\/search?q=content-type+headers&#038;rlz=1C5CHFA_enAU832AU832&#038;oq=content-type+headers&#038;aqs=chrome..69i57j0l7.6429j0j4&#038;sourceid=chrome&#038;ie=UTF-8'>Content-Type<\/a>: text\/plain<\/i> to <i>Content-Type: text\/html<\/i> and that step alone opens the door to <a target_blank title'?' href='https:\/\/www.youtube.com\/watch?v=EK_LN3XEcnw'>&#8220;a little bit of&#8221;<\/a> style in the body section of the email &#8230; but &#8230;<\/li>\n<li>given a lot of CSS3 is not supported in HTML Inline Email styling we had to forgo &#8230;<br \/>\n<code><br \/>\n style=\"background:linear-gradient(top,pink,yellow);\"<br \/>\n<\/code><br \/>\n &#8230; for a linear-gradient background &#8230; and &#8230;<br \/>\n<code><br \/>\n&lt;head&gt;&lt;style&gt; tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} &lt;\/style&lt;\/head&gt;<br \/>\n<\/code><br \/>\n &#8230; for alternate table row colouring (as per the heads up at <a target=_blank title='Useful link' href='https:\/\/www.w3.org\/Style\/Examples\/007\/evenodd.en.html'>this link<\/a>, thanks) &#8230; for good &#8216;ol &#8230;<\/p>\n<ol>\n<li><a target=_blank title='Linux sed command information from computerhope' href='http:\/\/www.computerhope.com\/unix\/used.htm'>sed<\/a> (thanks to <a target=_blank title='Useful link, thanks' href='https:\/\/stackoverflow.com\/questions\/23106621\/replace-multiple-consecutive-white-spaces-with-one-comma-in-unix'>this useful link<\/a> for advice)<\/li>\n<li><a target=_blank title='awk' href='https:\/\/www.gnu.org\/software\/gawk\/manual\/gawk.html'>awk<\/a> (and its excellent <a target=_blank title='awk getline' href='https:\/\/www.gnu.org\/software\/gawk\/manual\/html_node\/Getline.html'><i>getline<\/i><\/a> method, and thanks to <a target=_blank title='Useful link' href='https:\/\/docs.freebsd.org\/info\/gawk\/gawk.info.Getline.html'>this useful link<\/a> for advice)<\/li>\n<\/ol>\n<p> &#8230; scripting interventions instead &#8230; so that for that &#8220;First Report&#8221; we output the report (largely) in an HTML table element as per &#8230;<br \/>\n<code><br \/>\n       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&lt;html&gt;&lt;head&gt;&lt;style&gt; tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} &lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;First Date ps-ef world.php Report at `date`&lt;\/h1&gt;&lt;table style='border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'&gt;&lt;tr&gt;&lt;th&gt;UID&lt;\/th&gt;&lt;th&gt;PID&lt;\/th&gt;&lt;th&gt;PPID&lt;\/th&gt;&lt;th&gt;C&lt;\/th&gt;&lt;th&gt;STIME&lt;\/th&gt;&lt;th&gt;TTY&lt;\/th&gt;&lt;th&gt;TIME&lt;\/th&gt;&lt;th&gt;CMD&lt;\/th&gt;&lt;\/tr&gt;`cat date_ps_ef_first.txt | sed '\/^\/s\/\/&lt;tr&gt;&lt;td&gt;\/g' | sed 's\/ \\{1,\\}\/&lt;\\\/td&gt;&lt;td&gt;\/g' | awk '{ t=0; while (getline \\$0) { if (t == 0) { print \\\"&lt;tr style=background-color:#e7e7e7 !important;\\\" substr(\\$0,3);  } else { print \\$0; } t=1-t; } }' | sed '\/$\/s\/\/&lt;\\\/td&gt;&lt;\\\/tr&gt;\/g'`&lt;\/table&gt;&lt;\/body&gt;&lt;\/html&gt;\" | sendmail -t -f rmetcalfe15@gmail.com<br \/>\n<\/code><br \/>\n &#8230; and for the &#8220;Changed Report&#8221; we output the report (largely) in an HTML textarea element as per &#8230;<br \/>\n<code><br \/>\n       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&lt;html&gt;&lt;body&gt;&lt;h1&gt;Changed Date ps-ef world.php Report at `date`&lt;\/h1&gt;&lt;textarea rows=${cur} style='background-color:#e7e7e7;border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'&gt;`diff date_ps_ef_first.txt date_ps_ef_next.txt`&lt;\/textarea&gt;&lt;\/body&gt;&lt;\/html&gt;\" | sendmail -t -f rmetcalfe15@gmail.com<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p>To get to the HTML textarea element stage <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Mac\/date_ps_ef.ksh-GETME\">we made these changes<\/a>, and then to progress onto a tabular &#8220;First Report&#8221; <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Mac\/date_ps_ef.ksh--GETME\">we applied these changes<\/a> to the final <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/date_ps_ef.ksh--GETME\">date_ps_ef.ksh<\/a> Korn Shell script that finished up on the rjmprogramming.com.au web server, again, started via &#8230;<\/p>\n<p><code><br \/>\nksh date_ps_ef.ksh &gt; \/dev\/null 2&gt; \/dev\/null &amp;<br \/>\n<\/code><\/p>\n<p> &#8230; to &#8220;do its th<strike>a<\/strike>ing&#8221; in the background, quietly, like.<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/linux-sendmail-content-type-tutorial\/'>Linux sendmail Content Type Tutorial<\/a>.<\/p-->\n<hr>\n<p id='lpt'>Previous relevant <a target=_blank title='Linux sendmail Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/linux-sendmail-primer-tutorial\/'>Linux sendmail Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/sendmail_command_line.pdf\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux sendmail Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/sendmail_command_line.jpg\" title=\"Linux sendmail Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux sendmail Primer Tutorial<\/p><\/div>\n<p>The <a title='Linux mailx Primer Tutorial' href='#lpt'>Linux mailx Primer Tutorial<\/a> outlined some very useful Linux (and Unix and perhaps macOS (or Mac OS X)) command line tools to email useful information to emailees.<\/p>\n<p>Today, we want to outline a <a target=_blank title='Linux sendmail' href='https:\/\/linux.die.net\/man\/8\/sendmail.sendmail'>sendmail<\/a> alternative approach for reporting purposes, perhaps, again, from the command line, and we&#8217;d like to thank <a target=_blank title='Useful link' href='https:\/\/www.linuxquestions.org\/questions\/linux-newbie-8\/sendmail-subject-and-body-4175623014\/'>this link<\/a> for great advice, here.<\/p>\n<p>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&#8217;s &#8220;<a target=_blank title='Linux ps' href='http:\/\/man7.org\/linux\/man-pages\/man1\/ps.1.html'>ps<\/a> -ef&#8221; mentions of &#8220;\/world.php&#8221; (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 &#8230;<\/p>\n<ul>\n<li>usefulness of Linux <a target=_blank title='Linux echo' href='http:\/\/man7.org\/linux\/man-pages\/man1\/echo.1.html'>echo<\/a>&#8216;s -e switch &#8230;<br \/>\n<blockquote cite='http:\/\/man7.org\/linux\/man-pages\/man1\/echo.1.html'><p>\n-e     enable interpretation of backslash escapes\n<\/p><\/blockquote>\n<p> &#8230; to help out the construction of sendmail headers that require line feed characters to work\n<\/li>\n<li>usefulness of <a target=_blank title='Linux backtick' href='https:\/\/unix.stackexchange.com\/questions\/27428\/what-does-backquote-backtick-mean-in-commands'>Linux\/Unix backtick<\/a> to include varieties of data into the <i>subject<\/i> and <i>body<\/i> sections of the email<\/li>\n<li>usefulness of sendmail <a target=_blank title='Linux sendmail headers' href='https:\/\/serverfault.com\/questions\/347602\/sending-e-mail-from-sendmail-with-headers'>headers<\/a> featuring heavily in the Korn Shell Email (from command line) commands <font color=blue>as per<\/font> &#8230;<br \/>\n<code><br \/>\necho -e \"<font color=blue>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`<\/font>\" | sendmail -t -f rmetcalfe15@gmail.com<br \/>\necho -e \"<font color=blue>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`<\/font>\" | sendmail -t -f rmetcalfe15@gmail.com<br \/>\n<\/code>\n<\/li>\n<li>usefulness of <a target=_blank title='Linux &#038; background processing' href='https:\/\/www.google.com\/search?q=Linux+%26+background+process&#038;rlz=1C5CHFA_enAU832AU832&#038;oq=Linux+%26+background+process&#038;aqs=chrome..69i57j0l7.8476j0j7&#038;sourceid=chrome&#038;ie=UTF-8'>&#038; background processing<\/a> as per the &#8230;<br \/>\n<code><br \/>\nksh date_ps_ef.ksh &gt; \/dev\/null 2&gt; \/dev\/null &amp;<br \/>\n<\/code><br \/>\n &#8230; we ended up setting off our <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/date_ps_ef.ksh_GETME\">date_ps_ef.ksh<\/a> Korn Shell Script reporter &#8230;<br \/>\n<code><br \/>\n#!\/bin\/ksh<br \/>\n# date_ps_ef.ksh<br \/>\n# Report on world.php<br \/>\nlastcur=\"\"<br \/>\nwhile [ 0 -lt 1 ]; do<br \/>\n   cur=\"`ps -ef | grep '\/world.php' | grep -v 'grep ' | wc -l`\"<br \/>\n   echo \"`date` $cur\"<br \/>\n   if [ \"$lastcur\" != \"$cur\" ]; then<br \/>\n     if [ -z \"$lastcur\" ]; then<br \/>\n       echo \"`ps -ef | grep '\/world.php' | grep -v 'grep '`\" &gt; date_ps_ef_first.txt<br \/>\n       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<br \/>\n     else<br \/>\n       echo \"`ps -ef | grep '\/world.php' | grep -v 'grep '`\" &gt; date_ps_ef_next.txt<br \/>\n       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<br \/>\n       cat date_ps_ef_next.txt &gt; date_ps_ef_first.txt<br \/>\n     fi<br \/>\n     lastcur=\"$cur\"<br \/>\n   fi<br \/>\n   # ps -ef | grep '\/world.php' | grep -v 'grep '<br \/>\n   sleep 30<br \/>\ndone<br \/>\nexit<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p>Take a look at some of all this in action with today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/sendmail_command_line.pdf\">PDF presentation<\/a>.<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/linux-sendmail-primer-tutorial\/'>Linux sendmail Primer Tutorial<\/a>.<\/p-->\n<hr>\n<p id='lpt'>Previous relevant <a target=_blank title='Linux mailx Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/linux-mailx-primer-tutorial\/'>Linux mailx Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/mailx\/Linux_mailx_uuencode.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux mailx Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/mailx\/Linux_mailx_uuencode.jpg\" title=\"Linux mailx Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux mailx Primer Tutorial<\/p><\/div>\n<p>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 &#8230; 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.<\/p>\n<p><font color=blue>Background reading for tutorial:<\/p>\n<ul>\n<li><a target=_blank title='mailx' href='http:\/\/linux.die.net\/man\/1\/mailx'>mailx<\/a><\/li>\n<li><a target=_blank title='uuencode' href='http:\/\/linux.die.net\/man\/1\/uuencode'>uuencode<\/a><\/li>\n<\/ul>\n<p><\/font>\n<\/p>\n<p>Here is a tutorial that introduces you to some email with attachment, subject and body text from the Linux <a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/Linux\/mailx\/Linux_mailx_uuencode.jpg'>command line<\/a>.<\/p>\n<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d4673' onclick='var dv=document.getElementById(\"d4673\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d4673' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d49008' onclick='var dv=document.getElementById(\"d49008\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/email\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d49008' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d49016' onclick='var dv=document.getElementById(\"d49016\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/command-line\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d49016' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s Linux sendmail Primer Tutorial&#8216;s command line emailing really felt &#8220;command line&#8221;. We do not consider this an insult per se, and we tend towards &#8220;the badge of honour thinking&#8221; here. But given reports are more trusted when impactive (we &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-sendmail-content-type-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[122,234,1667,281,3299,380,1996,576,677,707,997,3300,1115,1118,1319,1339],"class_list":["post-49016","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-awk","tag-command-line","tag-content-type","tag-css","tag-echo","tag-email","tag-header","tag-html","tag-korn-shell","tag-linux","tag-programming","tag-ps","tag-sed","tag-sendmail","tag-tutorial","tag-unix"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/49016"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=49016"}],"version-history":[{"count":6,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/49016\/revisions"}],"predecessor-version":[{"id":49022,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/49016\/revisions\/49022"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=49016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=49016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=49016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}