Wednesday, April 01, 2009

Sending a HTML file inline with Subject using  Sendmail command !

echo "Subject: Testing" | cat - file.html-old| /usr/lib/sendmail your-mail@mailid.com

Nawk to convert CSV file in HTML column/row format



/usr/bin/awk has a limit for the printf string of 398 characters.
/usr/xpg4/bin/nawk has no limit.

Nawk  to convert CSV file in HTML column/row format

nawk 'BEGIN{
FS=","
print  "MIME-Version: 1.0"
print  "Content-Type: text/html"
print  "Content-Disposition: inline"
print  "<HTML>""<TABLE border="1"><TH>SA TEAM</TH><TH>Host Name</TH><TH>Host ID</TH><TH>User ID</TH><TH>Login Shell</TH><TH>GCOS Field</TH><TH>Data Source</TH><TH>Domain Name</TH><TH>Acct Status</TH><TH>Lock Type</TH>"
}
 {
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }
' file-to-convert.csv > file.html


Sending HTML Mail by way of report !!!


Sending  HTML Mail by way of report !!!

I wanted to do below :
Step 1) Convert .csv comma seperated file to .html file
Step 2) Sending the HTML file as inline mail (had to use sendmail command , mailx wont support sending mail by way of HTML inline):
 
 

Step 1) Convert .csv comma seperated file to .html file

CSV file :test.csv: (This  need to convert to .html)
EQADM,edtsdb445s4s,832caef0,gpatmon,/bin/ksh,prete Erick Whindleton
EQADM,eqzd56s5s,83cd26bd,gpatmon,/bin/ksh,rete WHINDLETON,KED_RA,none
EQADM,eqzrtshs,8343f9a5,gpatmon,/bin/ksh,hshsrick Whindleton
EQADM,ed876b4c,832cab9c,gpatmon,/bin/ksh,sjsjs Erick Whindleton
EQADM,eq765qa25-phys,83c3802d,gpatmon,/bin/ksh,tsgsgsgWHINDLETON
EQADM,eq234canj1,80c9b573,gpatmon,/bin/ksh,ERICK C WHINDLETON
EQADM,eqznj10-phys,83193e0b,gpatmon,/bin/ksh,Erick Whinrtsrsksksk
EQADM,eqzdsnj2-phys,8338abba,gpatmon,/bin/ksh,ERICK C WHINDLETON

For this I used awk command :

bash-2.03$ awk 'BEGIN{
FS=","
print  "<HTML>""<TABLE border="1"><TH>one</TH><TH>two</TH><TH>three</TH><TH>four</TH><TH>five</TH><
TH>six</TH><TH>seven</TH><TH>eight</TH><TH>nine</TH><TH>ten</TH>"
}
 {
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }
' test.csv > file.html


Step 2) sending the HTML file as inline mail :

#!/usr/bin/ksh

export MAILTO=sendmailto@mailid.com
export CONTENT="index.html"
export SUBJECT="Html Format"
(
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $CONTENT
) | /usr/sbin/sendmail $MAILTO