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

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

Related

Shell script output in tabular format

I have an output from my script -
$cat file.txt
Jobname Date Time Status
abc 12/9/11 17:00 Completed
I have used the below code to get the output in tabular format
awk 'BEGIN{print ""} {print "";for(i=1;i<=NF;i++)print "" $i"";print ""} END{print ""}' file.txt > file.html
$cat file.html
And then I am sending this to my mail
(
echo "From: XXXX#oooo.com "
echo "To: bbbb#oooo.com "
echo "MIME-Version: 1.0"
echo "Subject: Test HTML e-mail."
echo "Content-Type: text/html"
cat file.html
) | sendmail -t
I am getting the output in my mail in this format
Whereas I am expecting something like -
you should add a border to your table, eg :
<style>
table, th, td {
border: 1px solid black;
}
</style>

convert text file to html ouput so that data can be printed in table format

I have a text file which has 3 values separated by :
25-08-2019_19.00.00 : Port port1 of URL http://ip1:port1/ is NOT OPEN : Zoom1
25-08-2019_19.00.00 : Port port2 of URL http://ip2:port2/ is NOT OPEN : MP
and so on.
I want to print the output to a html type tabular format file, which has 3 headings, date, output and system and corresponding data in 3 columns.
I tried below code, but it is not putting data to table format.
#! /bin/bash
if [ $# -eq 0 ] ; then
echo "USAGE: $(basename $0) file1 file2 file3 ..."
exit 1
fi
for file in $* ; do
html=$(echo $file | sed 's/\.txt$/\.html/i')
echo "<html>" >> $html
echo "<style type="text/css">
table, th, td {
border: 1px solid black;
}
</style>" >> $html
echo " <body>" >> $html
echo '<table>' >> $htm
echo '<th>HEADING1</th>' >> $html
echo '<th>HEADING2</th>' >> $html
echo '<th>HEADING3</th>' >> $html
while IFS=':' read -ra line ; do
echo "<tr>" >> $html
for i in "${line[#]}"; do
echo "<td>$i</td>" >> $html
# echo "<td>$i</td>" >> $html
done
echo "</tr>" >> $html
done < $file
echo '</table>'
echo " </body>" >> $html
echo "</html>" >> $html
done
If you can, please consider awk:
awk -F' : ' '
BEGIN{
print "<html><style type=\"text/css\">table, th, td {border: 1px solid black;}</style><body><table><th>HEADING1</th><th>HEADING2</th><th>HEADING3</th>"
}
{
print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i "</td>"
print "</tr>"
}
END{
print "</table></body></html>"
}
' input_file > output.html
The BEGIN and END statement fills the static html tags.
The middle statement fills one line of the array according to each input file line by adding <tr> for each line and <td> for each field.

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

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

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.

need to send html mail with attachment in unix

I am facing problems in sending html mail with attachemnt.I will able to send mail with
attachment (plain text ) using mailx -s and uuencode command and also html mail
without attachment using sendmail option.
However I am not able to send html mail along with attachment.
Either one of it is working (html mail or attachment)
Below are the different ways I have tried. Could you please help me in resolving the same.
1) Failed because of illegal option base64
#!/usr/bin/ksh
export MAILTO="abc#abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
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 --base64 $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail $MAILTO
2)
cib-sokay2{u384283}323:cat test_html2.sh
{
uuencode query5_result.csv query5_result.csv > attachment.txt
cat mail.html attachment.txt > attachment2.html
} | /usr/lib/sendmail -t abc#abc.com
-----------------------------------------------
3)
cib-sokay2{u384283}324:cat test_html3.sh
export MAILTO="abc#abc.com"
export CONTENT="mail.html"
export CONTENT_F="attachment.txt"
export SUBJECT="TEST EMAIL: TESTING HTML"
BOUNDARY='=== This is the boundary between parts of the message. ==='
{
print - "From: Someone <$MAILFROM>"
print - "To: Someone <${MAILTO}>"
print - 'Subject:' $SUBJECT
print - 'MIME-Version: 1.0'
print - 'Content-Type: MULTIPART/MIXED; '
print - ' BOUNDARY='\"$BOUNDARY\"
print -
print - ' This message is in MIME format. But if you can see this,'
print - " you aren't using a MIME aware mail program. You shouldn't "
print - ' have too many problems because this message is entirely in'
print - ' ASCII and is designed to be somewhat readable with old '
print - ' mail software.'
print -
print - "--${BOUNDARY}"
print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII'
print -
cat $CONTENT
print -
print -
print - "--${BOUNDARY}"
print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='${CONTENT}
print - 'Content-Disposition: attachment; filename='${CONTENT_F}
print -
cat ${CONTENT}
print -
print - "--${BOUNDARY}--"
} | /usr/lib/sendmail ${MAILTO}
------------------------------------------------------------
cib-sokay2{u384283}326:cat test_html4.sh
#!/usr/bin/ksh
export MAILTO="abc#abc.com"
export CONTENT="mail.html"
export SUBJECT="subject of email"
(
echo "Subject: $SUBJECT"
# This appears in the mail body
cat $CONTENT
# The next line creates the attachment with a suitable extension to read
# with Windows notepad
unix2dos "attachment.txt" | uuencode myattach.txt
echo "."
) | /usr/lib/sendmail $MAILTO
-------------------------------------
Your first attempt is fairly close. Your second attempt seems to write something to a file which is then abandoned, and you feed empty input to sendmail. Your third attempt has a grave error in the MIME boundary and some peculiarities (why do you put the same content twice?), but is basically fairly similar to the first. Your fourth attempt looks fairly reasonable but old-fashioned, but could have failed because of false assumptions about how things need to be formatted (for example, if $CONTENT does not start with an empty line, your contents would go smack dab in the message headers, creating illegal headers and probably being rejected).
Assuming you have a command base64 on your system (it is included in GNU Coreutils since 6.0, almost ten years back), just replace the uuencode call with that, and fix the various minor formatting errors. Here is a refactored script which hopefully catches most of them.
#!/bin/sh
# No need to export anything, we are not passing these to child processes
MAILTO="abc#abc.com"
SUBJECT="Mail Subject"
BODY="card_summary_mail.html"
ATTACH="query5_result.csv"
( cat <<____HERE
To: $MAILTO
Subject: $SUBJECT
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"
... You could put a MIME preamble here but nobody ever reads it ...
---q1w2e3r4t5
Content-Type: text/html
Content-Disposition: inline
X-Notice: you need an empty line before the body here:
____HERE
cat "$BODY"
# also notice empty line at beginning of next snippet
cat <<____THERE
---q1w2e3r4t5
X-Notice: just "application" is incompletely specified
Content-Type: application/octet-stream; name="$(basename "$ATTACH")"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$(basename "$ATTACH")"
X-Notice: another empty line
____THERE
base64 "$ATTACH"
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail "$MAILTO"
However, assembling your own MIME structure gets tired really fast -- I would recommend looking for a command-line MUA with proper attachment support (mutt is popular; some modern mail/mailx clones have similar facilities, but it's not standard).
Also, there is nothing ksh-specific here, so I changed the shebang.
Your first attempt is almost correct. Just replace --base64 by -m as shown below:
#!/usr/bin/ksh
export MAILTO="abc#abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
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/lib/sendmail $MAILTO