RE: output from script into email

  • From: Phil Sumner <phil.sumner@xxxxxxxxx>
  • To: "Brian.Zelli@xxxxxxxxxxxxxxx" <Brian.Zelli@xxxxxxxxxxxxxxx>, "oracle-l (oracle-l@xxxxxxxxxxxxx)" <oracle-l@xxxxxxxxxxxxx>
  • Date: Mon, 3 Aug 2015 08:40:16 +0000

I much prefer to explicitly send emails with result sets intended to be read by
people as HTML, it gives cleaner looking results in most modern mail clients.
Unless you've got people using text-based email clients, in which case it all
goes a bit weird.

I use a generic bash script which calls an SQL script with the same base name
as the script and is set up to spool HTML output to a file (name passed as a
parameter):

TEMPFILE=$(mktemp)
# Temporary file name
SQLFILE="${0%.sh}.sql"
# SQL file - basename with .sh
sqlplus -s / as sysdba @"${SQLFILE}" "${TEMPFILE}"

It then takes the output and builds up a properly formatted HTML email:

HEADERS="From: ${MAILFROM}\n"
HEADERS+="To: ${MAILTO}\n"
HEADERS+="Subject: ${SUBJECT}\n"
HEADERS+="Content-Type: text/html\n"
HEADERS+="MIME-Version: 1.0\n"
HEADERS+="\n"

# Build up email
HEADERS="From: ${MAILFROM}\n"
HEADERS+="To: ${MAILTO}\n"
HEADERS+="Subject: ${SUBJECT}\n"
HEADERS+="Content-Type: text/html\n"
HEADERS+="MIME-Version: 1.0\n"
HEADERS+="\n"

PREAMBLE="<p>Below is the ${REPORTNAME} for ${ORACLE_SID} on host
${HOSTNAME}</p>"
BODY+="$(< $TEMPFILE)"
FOOTER="<small><em>Generated by $0 running as user ${USER}, for SID
${ORACLE_SID} on ${HOSTNAME}</em></small>"

(
printf '%b\n' "${HEADERS}"
printf '%b' "${PREAMBLE}"
printf '%b' "${BODY}"
printf '%b' "${FOOTER}"
) | /usr/sbin/sendmail -t

Regards,
--
Phil Sumner
Oracle Database Administrator
phil.sumner@xxxxxxxxx<mailto:phil.sumner@xxxxxxxxx>



From: oracle-l-bounce@xxxxxxxxxxxxx [mailto:oracle-l-bounce@xxxxxxxxxxxxx] On
Behalf Of Zelli, Brian
Sent: 31 July 2015 19:50
To: oracle-l (oracle-l@xxxxxxxxxxxxx)
Subject: output from script into email

I am sending myself a space report and it isn't formatting very well. I use:

mail -t "Mail Subject" myemailaddress < sp_used.txt

The sp_used.txt is the output of a script I run. How do I format it better. I
saw uuencode but can't seem to use it correctly in my mail statement.

Brian


This email message may contain legally privileged and/or confidential
information. If you are not the intended recipient(s), or the employee or agent
responsible for the delivery of this message to the intended recipient(s), you
are hereby notified that any disclosure, copying, distribution, or use of this
email message is prohibited. If you have received this message in error, please
notify the sender immediately by e-mail and delete this email message from your
computer. Thank you.

Other related posts: