send inline html charts and diagrams via unix - unix

I am trying to generate some report and send it through unix. This report contains PIE-charts any idea how to inline send charts in mail body.
I tried below code. HTML is working in browser but after I send the HTML over mail it does not shows anything in mail body.
`MAILTO="x#a.com"
FROM='y#x.com'
CONTENT="pi.html"
SUBJECT="test"
(
echo "To:$MAILTO"
echo "From:$FROM"
echo "Cc:$CC"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $CONTENT
) | /usr/sbin/sendmail -t`
content of pi.html ( I have copied the content from the url and pasted in pi.html)
https://developers.google.com/chart/interactive/docs/gallery/piechart
Please suggest a way to handle this.

Related

sendmail with zip is corrupting first file in the zip

I am trying to send a mail with zip file attached from an unix box. I am limited to use sendmail utility. i ziped the files using the command
zip test.zip 1.html 2.html 3.html
and when trying to send mail with below commands. One of the three files (first file) is not opening properly. but the rest 2.html and 3.html is working fine.
I am getting the error as "Unavailable Data: 1.html"
(
echo "From: from#from.com"
echo "To: to#to.com"
echo "Subject: subject"
echo "Mime-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="X12345"'
echo '--X12345'
echo "Content-Type: application/zip;"
echo "Content-Transfer-Encoding: base64"
echo "Content-Disposition: attachement; filename=test.zip"
base64 test.zip
echo '--X12345'
) | sendmail -t
Can some please help. Thanks in advance.
You failed to provide empty line to mark end of main headers and end of mime part header.
(
cat - <<END
From: from#from.com
To: to#to.com
ubject: subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="X12345"
--X12345
Content-Type: application/zip;
Content-Transfer-Encoding: base64
Content-Disposition: attachement; filename=test.zip
END
base64 test.zip
echo '--X12345'
) | /usr/sbin/sendmail -t

send email with attachment using Unix

I am using the following script to send an email from Unix sever. I have a requirement to attach a file which is at the same location where script is available - /home/app111/attachment.csv.
Could you please help me how to send the file in attachment?
`CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
awk ' BEGIN {
print "To: XXXX#gmail.com"
print "From: YYYY#gmail.com"
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: PO file '$CUR_DATE'"
print "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
print "<br>Thanks,</br></font></body></html>"
} ' | sendmail -t`
1
Using mailx -a if mailx -a feature is supported
2
Using uuencode filenm filenm | mailx s#abc.com
3
mutt -a filenm a#abc.com
If you really want to use sendmail (and not mail or mutt), you'll have to encode your attachment in base64 and concatenate it to your message, along with boundaries and the whole nine yards. There is a great article describing exactly what you want to do here with a code example:
http://backreference.org/2013/05/22/send-email-with-attachments-from-script-or-command-line/
If you're on a flavor of Unix or Linux that has mutt or mail, I would definitely recommend one of the two instead of sendmail as it will be a lot easier (and these solutions are also described in the posted article). Here is an example of how you could do it with mail:
CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
to="XXXX#gmail.com"
from="YYYY#gmail.com"
content_type="text/html"
file_to_attach="/home/app111/attachment.csv"
subject="PO file '$CUR_DATE'"
read -r -d '' body << 'EOF'
<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>
<br>Thanks,</br></font></body></html>
EOF
mail -A "$file_to_attach" --content-type "$content_type" -s "$subject" -r "$from" "$to" <<< "$body"
Try this:
MAILFROM="YYYY#gmail.com"
MAILTO="XXXX#gmail.com"
SUBJECT="PO file '$CUR_DATE'"
MAILPART_BODY=q1w2e3r4t5 ## Generates Unique ID
MAILPART=q1qw2ew3r4t35 ## Generates Unique ID
ATTACH="/home/app111/attachment.csv"
(
echo "From: $MAILFROM"
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
echo ""
echo "--$MAILPART"
echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
echo ""
echo "--$MAILPART_BODY"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "You need to enable HTML option for email"
echo "--$MAILPART_BODY"
echo "Content-Type: text/html; charset=ISO-8859-1"
echo "Content-Disposition: inline"
echo "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
echo "<br>Thanks,</br></font></body></html>"
echo "--$MAILPART_BODY--"
echo "--$MAILPART"
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
echo ""
uuencode $ATTACH $(basename $ATTACH)
echo "--$MAILPART--"
) | sendmail -t

How to attach multiple html files in Email body from unix

I am trying to generate and send html files by attaching to email body. I tried using awk for generating and sending one file. eg. the input file MARTINI has these records:
1554894,2015-04-16,00:21:52,processes.martini_gsicorptradeeventoutput.instancecount,0,1,UP
1554793,2015-04-15,22:03:52,processes.martini_gsicorptradeeventoutput.instancecount,2,0,DOWN
and I have this awk in a file named HTML:
awk 'BEGIN {
FS=","
print "MIME-Version: 1.0"
print "To:lijo#abc.com"
print "From:lijo#abc.com"
print "Subject: Health check"
print "Content-Type: text/html"
print "Content-Disposition: inline"
print "<HTML>""<TABLE border="1"><TH>Ref_id</TH><TH>EOD</TH><TH>Time</TH><TH>Process</TH><TH>Desc</TH><TH>Instance</TH><TH>Status</TH>"
}
{
printf "`<TR>`"
for(i=1;i<=NF;i++)
printf "`<TD>%s</TD>`", $i
print "`</TR>`"
}
END {
print "`</TABLE></BODY></HTML>`"
} ' /home/martini > /home/martini_html
Later I send this file through email cat MARTINI_HTML | /usr/sbin/sendmail -t . This works until here. But now i have 2 new tasks
How to convert multiple files Say MARTINI1, MARTINI2 ... etc into html files and how to attach them in email body as separate table block and not as a single table. Assuming two files are attached then email body should look similar to the image attached.
Here's how to do it: cheat.
Send yourself an email with two attachments. Use that raw email as a template. That way you can skip all the discovery about MIME types and whatnot.

outputting errors to a logfile with echo output to the same logfile

I have a ksh script that contains a bunch of echo statements that output to a log file like this:
echo "[$(date '+%c')] some text of a status" >> $lgfile
I'm trying to output the errors to the same file but can't seem to get it to work. The ksh file gets started from another scipt like this:
lgfile="$(date '+%Y'-'%m'-'%d'_'%H':'%M'_${ID}).log"
echo "[$(date '+%c')] $ID is now started" >> $lgfile
. ./process.ksh $lgfile $ID
I've tried running it like this:
. ./process.ksh $lgfile $ID 2>> $lgfile
but that seems to add the start of the lgfile and remove some stuff. I want to stderror to just append to the lgfile
Redirect stderr to stdout.
echo "[$(date '+%c')] some text of a status" >> $lgfile 2>&1

How to send HTML body email with multiple text attachments using sendmail

I want to send a HTML file as message body and want to attach multiple text files to this email message.
Since html file needs to be sent, sendmail has to be used ( I could not do it using mailx ).
How do you send HTML body email and multiple text attachments using sendmail?
Assuming you have uunecode available in your system you can send email with multiple attachments like this:
#!/bin/bash
...
...
...
BOUNDARY="=== This is the boundary between parts of the message. ==="
{
echo "From: $MAILFROM"
echo "To: $MAILTO"
echo "Subject:" $SUBJECT
echo "MIME-Version: 1.0"
echo "Content-Type: MULTIPART/MIXED; "
echo " BOUNDARY="\"$BOUNDARY\"
echo
echo " This message is in MIME format. But if you can see this,"
echo " you aren't using a MIME aware mail program. You shouldn't "
echo " have too many problems because this message is entirely in"
echo " ASCII and is designed to be somewhat readable with old "
echo " mail software."
echo
echo "--${BOUNDARY}"
echo "Content-Type: TEXT/PLAIN; charset=US-ASCII"
echo
echo "This email comes with multiple attachments."
echo
echo
echo "--${BOUNDARY}"
echo "Content-Type: application/zip; charset=US-ASCII; name="${ZIPFILE}
echo "Content-Disposition: attachment; filename="`basename ${ZIPFILE}`
echo
uuencode $ZIPFILE $ZIPFILE
echo
echo "--${BOUNDARY}--"
echo "Content-Type: application/pdf; charset=US-ASCII; name="${PDFFILE}
echo "Content-Disposition: attachment; filename="`basename ${PDFFILE}`
echo
uuencode $PDFFILE $PDFFILE
echo
echo "--${BOUNDARY}--"
} | /usr/lib/sendmail -t
I don't think sendmail is going to help you with that. Go for a client like mutt, and do e.g. mutt -a file1 -a file2 -- recipient#do.main. Or go for perl.
Here is a bash script I use to send reports I generate to people. They are sent as attachments. Place your HTML in the "body" variable of the script. I will leave the parametrization of the variables up to you.
#!/bin/bash
function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
from="me.last#company.com"
to="some.one#companyBlah.com"
subject="Your Report my Lord"
boundary="=== Boundary ==="
body="The reports are attached to this email"
declare -a attachments
attachments=( "fileOne.out" "fileTwo.out" "fileThree.out" "file-et-cetera.out")
# Build headers
{
printf '%s\n' "From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"
--${boundary}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
$body
"
for file in "${attachments[#]}"; do
[ ! -f "$file" ] && echo "Attachment $file not found, omitting file" >&2 && continue
mimetype=$(get_mimetype "$file")
printf '%s\n' "--${boundary}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
"
base64 "$file"
echo
done
# print last boundary with closing --
printf '%s\n' "--${boundary}--"
} | sendmail -t -oi

Resources