How to send a html file using mails in shell script? - html

I want to store my matrix's data in a html file and send a email in outlook ,my code looks as follows:
printf "<!DOCTYPE html>"
printf "<html>"
printf "<head>"
printf "<style>"
printf "</style>"
printf "</head>"
printf "<body>"
printf "<table>"
printf "<tr>"
printf "<th>Total</th>"
printf "<th>StillFail</th>"
printf "<th>Pass</th>"
printf "<th>ScriptError</th>"
printf "<th>APIName</th>"
printf "</tr>"
printf "<tr>"
echo
for ((j=1;j<=num_rows;j++)) do
printf "<tr>"
for ((i=1;i<=num_columns;i++)) do
printf "<td>"
printf "${matrix[$j,$i]}"
printf "</td>"
printf "</tr>"
done
echo
done
printf "</tr>"
printf "</table>"
printf "</body>"
printf "</html>"
#mailx -a 'Content-Type: html' -s "my subject" test#example.com < output.html
mailx -s "TESTING MAIL"</home/test/example/output.html "test#example.com"
I want my output as a well aligned table. Can someone help me on this? TIA

You need to append content-type in header it can be done with -a flag in mailx
comment your last line and try this
mailx -a 'Content-Type: text/html' -s "Test MAIL" test#example.com</home/test/example/output.html
Edit :
As per the OP End error : content-type:texthtml no such file or
directory
To install mailutils in mac
Press Command+Space and type Terminal and press enter/return key.
Run in Terminal app:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
and press enter/return key. Wait for the command to finish.
Run:
brew install mailutils
reference : http://macappstore.org/mailutils/

Maybe you can use this for a base:
(
echo "To: ADRESSES"
echo "Subject: SUBJECT"
echo "Content-Type: text/html"
echo
echo "HTML code"
) | /usr/sbin/sendmail -F "NameOfSender" -t

Related

Bash shell script html css

while read row do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
When I execute this fraction of code it gives me this error:
./table_html.csv: line 36: syntax error near unexpected token `echo'
./table_html.csv: line 36: ` echo "<td>$valore</td>" >> $file'
How do I fix it?
I'm not very good with shell scripts, that's why I can not think of anything else.
(Ah I was working on this when the alarm for the oven went off) what I wanted to say was it's just the "do" in the wrong place after the while. So here's your code restated (with a file="filename" added to get it working):
file="test2.html"
while read row
do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
So it was just a line break before the do is all you needed :-)

Use arguments as a part of variable inside of function

If I type:
function chk_is_it_started(){
PROCC_NAME_$1="my_process_$1";
echo "PROCC_NAME_$1 is: $PROCC_NAME_$1";
PID_FILE_OF_APP_$1="/run/pidfile_$PROCC_NAME_$1.pid"
PATH_OF_PROCCESS_NAME_$1=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $12}'`
PID_NUMBER_OF_APP_$1=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $2}'`
NUMBER_OF_OCCURENCE_$1=`echo ${#PID_NUMBER_OF_APP_$1[#]}`
if [[ "$NUMBER_OF_OCCURENCE_$1" == 0 ]];then
echo -e "Proccess isn't started..\nNow process $PATH_OF_PROCCESS_NAME_$1 is running and I'm creating a PID file..."
python /emu/script/$PROCC_NAME_$1.py & disown & echo $! > $PID_FILE_OF_APP_$1
else
echo "Proccess is STARTRED"
fi
}
chk_is_it_started blabla;
I will got the error:
root#orangepipc:~# chk_is_it_started blabla;
Could not find the database of available applications, run update-command-not-found as root to fix this
PROCC_NAME_blabla=my_process_blabla: command not found
PROCC_NAME_blabla is: blabla
-bash: PID_FILE_OF_APP_blabla=/run/pidfile_blabla.pid: No such file or directory
Could not find the database of available applications, run update-command-not-found as root to fix this
PATH_OF_PROCCESS_NAME_blabla=: command not found
Could not find the database of available applications, run update-command-not-found as root to fix this
PID_NUMBER_OF_APP_blabla=: command not found
-bash: ${#PID_NUMBER_OF_APP_$1[#]}: bad substitution
Could not find the database of available applications, run update-command-not-found as root to fix this
NUMBER_OF_OCCURENCE_blabla=: command not found
Proccess is STARTRED
But it is not!
Where I'm Making the misstake?
If I'm using th ecode without function it work!
Thx
I found the solution...
function chk_is_it_started(){
PROCC_NAME="dht22_$1"
# echo "PROCC_NAME_$1 is: $PROCC_NAME"
PID_FILE_OF_APP="/run/pidfile_$PROCC_NAME.pid"
# echo "PID_FILE_OF_APP is: $PID_FILE_OF_APP"
PATH_OF_PROCCESS_NAME=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $12}'`
# echo "PATH_OF_PROCCESS_NAME is: $PATH_OF_PROCCESS_NAME"
PID_NUMBER_OF_APP=`ps -aux|grep $PROCC_NAME_$1|grep -v grep|awk -F" " '{print $2}'`
# echo "PID_NUMBER_OF_APP is $PID_NUMBER_OF_APP"
PID_NUMBER_OF_APP=( $PID_NUMBER_OF_APP )
# echo "PID_NUMBER_OF_APP is $PID_NUMBER_OF_APP"
NUMBER_OF_OCCURENCE=`echo ${#PID_NUMBER_OF_APP[#]}`
# echo "NUMBER_OF_OCCURENCE is: $NUMBER_OF_OCCURENCE"
if [[ "$NUMBER_OF_OCCURENCE" == 0 ]];then
echo -e "Proccess isn't started..\nNow process $PATH_OF_PROCCESS_NAME is running and I create a PID file..."
python /emu/script/$PROCC_NAME.py & disown & echo $! > $PID_FILE_OF_APP
# exit
else
echo "Proccess is STARTRED"
fi
if [[ "$NUMBER_OF_OCCURENCE" > 1 ]];then
echo -e "Process $PROCC_NAME.py is started more than 1x"
echo -e "Now killing all proccess one by one"
while [ "$NUMBER_OF_OCCURENCE" != "1" ];
do
echo "Usao sam u while"
PID_NUMBER_OF_APP=`ps -aux|grep $PROCC_NAME|grep -v grep|awk -F" " '{print $2}'`
echo "PID_NUMBER_OF_APP is: $PID_NUMBER_OF_APP"
PID_NUMBER_OF_APP=( $PID_NUMBER_OF_APP )
echo "PID_NUMBER_OF_APP is $PID_NUMBER_OF_APP"
NUMBER_OF_OCCURENCE=`echo ${#PID_NUMBER_OF_APP[#]}`
echo "NUMBER_OF_OCCURENCE is: $NUMBER_OF_OCCURENCE"
kill $PID_NUMBER_OF_APP
rm -fr $PID_FILE_OF_APP
done
echo -e "Starting process $PROCC_NAME.py and creating a PID file..."
python /emu/script/$PROCC_NAME.py & echo $! > $PID_FILE_OF_APP
fi
}
chk_is_it_started bla1
chk_is_it_started bla2
Btw saluting user who gave vote -1 to my question :)

How to convert 2d array into html file in bash/shell?

I have a bash script in which I have my data in 2d array now currently I am printing this matrix as follows:
f1="%$((${#num_rows}+1))s"
f2=" %12s"
printf "$f1" ''
printf "$f2" "Total"
printf "$f2" "Pass"
printf "$f2" "Stillfail"
printf "$f2" "Failure"
printf "$f2" "APIName"
printf "$f2"
echo
for ((j=1;j<=num_rows;j++)) do
printf "$f1" $j
for ((i=1;i<=num_columns;i++)) do
if [ i == 5 ]; then
printf "%-20s" ${matrix[$j,$i]}
else
printf "$f2" ${matrix[$j,$i]}
done
echo
done
I tried a lot but still there is alignment problem so now I want to create a html table to output this data.Can anyone guide me on this? Thanks in advance
Here is the complete script. I think this will fix the issue
#!/bin/bash
declare -a matrix
num_rows=6
num_columns=5
for ((i=1;i<=num_columns;i++)) do
for ((j=1;j<=num_rows;j++)) do
matrix[$i,$j]=$RANDOM
done
done
f1="%$((${#num_columns}+3))s"
f2=" %12s"
header=( "" "Total" "Pass" "Stillfail" "Failure" "APIName" )
printf "$f1|" 'No.'
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "%-20s|" ${header[$i]}
else
printf "$f2|" ${header[$i]}
fi
done
echo
for ((j=1;j<=num_rows;j++)) do
printf "$f1|" $j
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "%-20s|" ${matrix[$j,$i]}
else
printf "$f2|" ${matrix[$j,$i]}
fi
done
echo
done
echo
echo "-----HTML----"
echo "<table style="width:50%">"
printf "$f1" '<tr><th>No.</th>'
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "<th align=left>%s</th>" ${header[$i]}
else
printf "<th align=right>%s</th>" ${header[$i]}
fi
done
echo '</tr>'
for ((j=1;j<=num_rows;j++)) do
printf "<tr><td align=right>$f1</td>" $j
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "<td align=left>%s</td>" ${matrix[$j,$i]}
else
printf "<td align=right>%s</td>" ${matrix[$i,$j]}
fi
done
echo '</tr>'
done
echo "</tr></table>"

Sending HTML formatted emails using script from CentOS not working in MS Outlook

I am trying to send formatted HTML emails from my linux CentOS server (Amazon EC2) using a script. I am sending it to a gmail account and a work email account and we use Outlook 2016.
the email gets formatted in gmail ok but in outlook it prints out the full html tags too.
myScript
#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
EMAILS="myGmailAddressa#gmail.com, myWorkEmail#company.com"
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" > /tmp/testemail.html
echo "<html" >> /tmp/testemail.html
echo "<head>" >> /tmp/testemail.html
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" >> /tmp/testemail.html
echo "</head>" >> /tmp/testemail.html
echo "<body style=\"background-color:#99ccff;\" \"width:600px;\">" >> /tmp/testemail.html
echo "<table>" >> /tmp/testemail.html
echo "<tr style=\"height:30px;\">" >> /tmp/testemail.html
echo "<td><strong>Current Disk Size:</strong></td>" >> /tmp/testemail.html
echo "<td>$CURRENT</td>" >> /tmp/testemail.html
echo "</tr>" >> /tmp/testemail.html
echo "</table>" >> /tmp/testemail.html
echo "</body>" >> /tmp/testemail.html
echo "</html>" >> /tmp/testemail.html
(cat /tmp/testemail.html) | mail -s "$(echo -e "Test 7 MIME- HTML Formatted OUTPUT \nMIME-Version: 1.0 \nContent-Type: text/html")" $EMAILS EOF
Here is the content of testemail.html file after I run the script.
testemail.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="background-color:#99ccff;" "width:600px;">
<table>
<tr style="height:30px;">
<td><strong>Current Disk Size:</strong></td>
<td>85</td>
</tr>
</table>
</body>
</html>
Gmail
Outlook
NOTE:
I know I can just forward the emails from gmail to my work email but
that is NOT what I want to do. I know this will help others so I want it to be done correct.
Has anyone ever had this problem before and/or have a solution to how this can be fixed.
G
your script violates lot of mail rfc, so expected result is the body of your message is broken. there's no time to explain, just try to use mutt to build correct messsage body:
cat /tmp/testemail.html | mutt \
-e "set content_type=text/html" \
-e "set from=your#address.com" \
-e "set realname=daemon" \
-e "set send_charset=utf-8" \
-s "test subject" \
myGmailAddressa#gmail.com \
myWorkEmail#company.com
and don't forget install it before usage: yum install -y mutt.

How to send using with attachment and html body using mailx [duplicate]

This question already has answers here:
Mailx send html message [duplicate]
(6 answers)
Closed 4 years ago.
I want to send an e-mail using mailx which has an attachment and HTML code on the body.
Setting the Content-type to "text/html" mail comes with file binary content on the body instead of attached.
echo "" >> $MAILFILE
echo "<HTML>" >> $MAILFILE
echo "<HEAD>" >> $MAILFILE
echo "</HEAD>" >> $MAILFILE
echo "<BODY>" >> $MAILFILE
echo "<table border="1">" >>$MAILFILE
echo "<tr bgcolor=#C0C0C0>" >>$MAILFILE
echo "<td><b>OUTAGE</b></td>" >>$MAILFILE
echo "<td>$OUTAGE_COUNT</td>" >>$MAILFILE
echo "</tr> </table><br>" >>$MAILFILE
echo "</table>" >> $MAILFILE
echo "</BODY></HTML>" >> $MAILFILE
(uuencode outage.zip outage.zip;cat $MAILFILE) | mailx -s "Outage Payments Status
Content-type: text/html" $distro -- -f ${EMAIL_FROM}
Result - Body's email:
begin 644 outage.zip M4$L#!!0````(`,:$(T5RYN"0#P```.D#```3`!4`;W5T86=E7W!A>6UE;G1S M+F-S=E54"0`#)&X'5"1N!U15>`0`.GX1#5-0#`(3A5$P"D;!,`5<&%Y;65N=',N8W-V550%``,D;#=457#``%!+!08``````0`!`$X```!5 %```````` ` end
Instead use sendmail as follows :
#!/usr/bin/ksh
export MAILTO="spam#ebay.com"
export SUBJECT="Mail Subject"
export BODY="/tmp/email_body.html"
export ATTACH="/tmp/attachment.txt"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode -m $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO