Sending email with multiple files..

Hi.

Your first for loop is completely unnecessary. Remove it.

And you could change:

"${logfile}" | uuencode "${attachment}"

to

uuencode "$logfile "$attachment"

(the point of this is only for aesthetic reasons, so the attachment name looks nicer in the email)

Try adding an extension so that it's associated with a program:

i.e.

attachment=${logfile##*/}.txt
1 Like

@msraham
Please tell us what Operating System and version you are running.

The "mailx m" is a kludge with MIME headers to make normal unix mail readable by non-standard mail readers such as M$ Outlook.
The absence of "-m" in your version of "mailx" just means we really need to know what O/S you have.
Btw. Do you know if you have unix "sendmail" available?

Btw. I have absolutely no idea where these messages come from.
Bearing in mind the parallel threads on this subject, please take two steps back and post the entire script, the call to that script, and the exact output from that script.

---------- Post updated at 01:40 ---------- Previous update was at 01:34 ----------

@scottn

The two loops were deliberate.
I have made a major cock-up by copying the O/Ps code into a tested construct.

It would make sense as:

echo "CONDITION:  $( wc -l < ${logfile1})"
echo "ENCOUNTER: $( wc -l < ${logfile2})"
echo "PROCEDURE: $( wc -l < ${logfile3})"
echo "PROVIDER:   $( wc -l < ${logfile4})"

---------- Post updated at 01:46 ---------- Previous update was at 01:40 ----------

@msraham
I have no idea why you removed the "ux2dos" command from the script I posted. This invalidates the command line.
If you do not have the "ux2dos" command (and bearing in mind that some unixes spell the command differently), please post what Operating System you are running because it is clearly not a mainstream commercial unix.

The OS which i am working on is :
SunOS elect 5.10 Generic_142909-17 sun4u sparc SUNW,SPARC-Enterprise

When i use "mailx m", it throws error (as posted earlier)

For me the mailx without the -m delivers the uuencode output as a body text blob not an attachment. Maybe there is a way to work around using sendmail, or an open source mailx with -m or other mail client or other attachment capabilities.

A mail message is just a pile of text, but the clients keep you out of the headers. I have cheated using mailx -s to stick a subject, linefeed and then an importance into the header, so I suppose you could stick a whole collection of files into the header, just by emulating the text of a mail message with attachments located before the body (why not?)! After all, encoded files have no quotes in the output, so $2 is just a very long string. I have written shell scripts to disassemble email with attached word and excel, with a little help from c base 64 decoder and open source word and excel decoders, so assembling should not be that hard.

Google showed me mutt and smtp-cli, which look pretty easy to use:

How to send email from the Linux command line � Simple Help

smtp-cli � command line SMTP client

Man Page for mutt (All Section 1) - The UNIX and Linux Forums

The Mutt E-Mail Client

This has an interesting comment about uuencode being unstable in email, and in modern email base64 seems to be more common:

http://systembash.com/content/linux-command-line-attach-files-email/

The following script does needs some enhancements,

I am loading 4 files at one stretch.

1) If any of the input file is not found or missing, the script should stop loading data for other files, and should send an email notification to the user.
2) Can we use a condition here in this script like

rc=$?
if [ $rc == 2 ]
then
echo 'Bene_eligiblity table load finished with warnings/errors'
mv /space/dbexport/PHR/data/eligExport.csv /space/dbexport/PHR/archive/eligExport-`date +%Y%m%d`.csv
chmod 640 /space/dbexport/PHR/archive/eligExport-`date +%Y%m%d`.csv
/space/dbexport/PHR/log/bene_eligibility-`date +%Y%m%d`.log | to=id11355@noridian.com
basename=`basename $logfile`
subject="BENE ELIGIBILITY TABLE LOAD STATUS"
( echo 'BENE ELIGIBILTY LOAD PROCESS FINISHED WITH WARNINGS, Please refer to attached log file' ; uuencode $logfile $basename ) | mailx -s "$subj
ect" "$to"
status=`cat $logfile`
exit $rc
elif [ $rc == 0 ]
then
echo 'Bene_eligiblity table load Successful'
mv /space/dbexport/PHR/data/eligExport.csv /space/dbexport/PHR/archive/eligExport-`date +%Y%m%d`.csv
chmod 640 /space/dbexport/PHR/archive/eligExport-`date +%Y%m%d`.csv
while read myline
do
echo $myline
done < /space/dbexport/PHR/data/eligExport.csv
/space/dbexport/PHR/log/bene_eligibility-`date +%Y%m%d`.log | to=id11355@noridian.com
basename=`basename $logfile`
subject="BENE ELIGIBILITY TABLE LOAD STATUS"
( echo 'BENE ELIGIBILTY LOAD PROCESS FINISHED SUCCESSFULLY'; uuencode $logfile $basename; "${DATE}" ) | mailx -s "$subject" "$to"

*********
Actual Script
*********

/usr/local/pl/util_scripts/perlencrypt.pl -k /export/home/oracle/secure/ealgorithm -d /sdr1/system/$pwd_file | sqlldr $userName control=/space/dbexport/PHR/control/condition.ctl log=/space/dbexport/PHR/log/condition-`date +%Y%m%d`.log bad=/space/dbexport/PHR/bad/condition-`date +%Y%m%d`.bad 
/usr/local/pl/util_scripts/perlencrypt.pl -k /export/home/oracle/secure/ealgorithm -d /sdr1/system/$pwd_file | sqlldr $userName control=/space/dbexport/PHR/control/encounter.ctl log=/space/dbexport/PHR/log/encounter-`date +%Y%m%d`.log bad=/space/dbexport/PHR/bad/encounter-`date +%Y%m%d`.bad 
/usr/local/pl/util_scripts/perlencrypt.pl -k /export/home/oracle/secure/ealgorithm -d /sdr1/system/$pwd_file | sqlldr $userName control=/space/dbexport/PHR/control/procedure.ctl log=/space/dbexport/PHR/log/procedure-`date +%Y%m%d`.log bad=/space/dbexport/PHR/bad/procedure-`date +%Y%m%d`.bad
/usr/local/pl/util_scripts/perlencrypt.pl -k /export/home/oracle/secure/ealgorithm -d /sdr1/system/$pwd_file | sqlldr $userName control=/space/dbexport/PHR/control/provider.ctl log=/space/dbexport/PHR/log/provider-`date +%Y%m%d`.log bad=/space/dbexport/PHR/bad/provider-`date +%Y%m%d`.bad
logfile1="/space/dbexport/PHR/log/condition-`date +%Y%m%d`.log"
logfile2="/space/dbexport/PHR/log/encounter-`date +%Y%m%d`.log"
logfile3="/space/dbexport/PHR/log/procedure-`date +%Y%m%d`.log"
logfile4="/space/dbexport/PHR/log/provider-`date +%Y%m%d`.log"
touch "${logfile1}"
touch "${logfile2}"
touch "${logfile3}"
touch "${logfile4}"
subject="CONDITION, ENCOUNTER, PROCEDURE & PROVIDER LOAD PROCESS FINISHED SUCCESSFULLY."
(
echo "The log files are attached to the e-mail"
echo ""
echo "CONDITION:" $( wc -l < /space/dbexport/PHR/data/conditionExport_test.csv);
echo "ENCOUNTER:" $( wc -l < /space/dbexport/PHR/data/encounterExport.csv); 
echo "PROCEDURE:" $( wc -l < /space/dbexport/PHR/data/procedureExport.csv);
echo "PROVIDER:" $( wc -l < /space/dbexport/PHR/data/providerExport.csv);
for logfile in "${logfile1}" "${logfile2}" "${logfile3}" "${logfile4}"
do
attachment=`basename "${logfile}"`
uuencode "${attachment}" < "${logfile}"
echo ""
done
) | mailx -s "${subject}" "$to" id11355@gmail.com
#status=`cat $logfile`

Yes, numbers on variables are a clue that the whole script should be:

 
for x in condition encounter procedure provider
do
 ...
 attach_arg="$attach_arg -a $this_file"
done
mutt $attach_arg . . . .

how do i pass these variables in the current script ?

Appreciate your help...

Do one x at a time (or parallel), except the email, and everywhere you say condition say ${x}. If you want to say CONDITION, you can put:

$( echo $x |tr '[a-z]' '[A-Z]' )

I just got a mutt binary and installed it, and JDK 6.22 at the same time. The right tool saves a ton of code. And non-root depot installs on on HPUX are tough.

I did some tweeking to the exisiting script.. and its working fine but not really... Please help..

The script below is for loading data from input file into the Oracle table.
There are 4 steps that it needs to go through, one after the other.
First Condition, then Encounter, then Procedure and then Provider.

From this script, "Condition" runs fine, and then it stops (end's).

Can anyone pls tell me as to what is wrong here with this script, that it should not stop, but continue to the next step.

 
#Condition
/usr/local/pl/util_scripts/perlencrypt.pl -k /export/home/oracle/secure/ealgorithm -d /sdr1/system/$pwd_file | sqlldr $userName control=/space/dbex
port/PHR/control/condition.ctl log=/space/dbexport/PHR/log/condition-`date +%Y%m%d`.log bad=/space/dbexport/PHR/bad/condition-`date +%Y%m%d`.bad
rc=$?
if [ $rc == 2 ]
then
echo 'Condition table load finished with warnings/errors'
#mv /space/dbexport/PHR/data/conditionExport_test.csv /space/dbexport/PHR/archive/conditionExport_test-`date +%Y%m%d`.csv
#chmod 640 /space/dbexport/PHR/archive/conditionExport_test-`date +%Y%m%d`.csv
#basename=`basename $logfile`
subject="CONDITION TABLE LOAD FINISHED WITH WARNINGS"
(
echo "The log file is attached to e-mail"
for logfile in "${logfile1}"
do
attachment=`basename "${logfile1}"`
uuencode "${attachment}" < "${logfile1}"
echo ""
done
) | mailx -s "${subject}" "$to" id11355@noridian.com
exit $rc
elif [ $rc == 0 ]
then
echo 'CONDITION table load Successful'
#mv /space/dbexport/PHR/data/conditionExport_test.csv /space/dbexport/PHR/archive/conditionExport_test-`date +%Y%m%d`.csv
#chmod 640 /space/dbexport/PHR/archive/conditionExport_test-`date +%Y%m%d`.csv
subject="CONDITION TABLE LOAD FINISHED SUCCESSFULLY"
(
echo "The log file is attached to e-mail"
echo "Total Condition records loaded:" $( wc -l < /space/dbexport/PHR/data/conditionExport_test.csv);
for logfile in "${logfile1}"
do
attachment=`basename "${logfile1}"`
uuencode "${attachment}" < "${logfile1}"
echo ""
done
) | mailx -s "${subject}" "$to" id11355@noridian.com
fi
exit $rc
 
#Encounter
/usr/local/pl/util_scripts/perlencrypt.pl -k /export/home/oracle/secure/ealgorithm -d /sdr1/system/$pwd_file | sqlldr $userName control=/space/dbex
port/PHR/control/encounter.ctl log=/space/dbexport/PHR/log/encounter-`date +%Y%m%d`.log bad=/space/dbexport/PHR/bad/encounter-`date +%Y%m%d`.bad
rc=$?
if [ $rc == 2 ]
then
echo 'Encounter table load finished with warnings/errors'
#mv /space/dbexport/PHR/data/encounterExport.csv /space/dbexport/PHR/archive/encounterExport-`date +%Y%m%d`.csv
#chmod 640 /space/dbexport/PHR/archive/encounterExport-`date +%Y%m%d`.csv
#basename=`basename $logfile`
subject="ENCOUNTER TABLE LOAD FINISHED WITH WARNINGS"
(
echo "The log file is attached to e-mail"
for logfile in "${logfile2}"
do
attachment=`basename "${logfile2}"`
uuencode "${attachment}" < "${logfile2}"
echo ""
done
) | mailx -s "${subject}" "$to" id11355@gmail.com
exit $rc

I don't remember you posting what Shell you are using but it seems to be one which supports "==".

Without trying to understand more random code in depth, you may need to look at the "man" page for your Shell and read up about "exit" and what it does.

1 Like

Yes, and logically, if you get not 0 or 2, something is amiss, so you might want to structure error handling like this:

 
case $rc in
(0)
  Success processing . . . .
  ;;
(2)
  Warning Processing . . .
  exit $rc
  ;;
(*)
  Fatal Error Processing . . .
  exit $rc
  ;;
esac
1 Like