Having issues to sending emails with attachment

29 views Asked by At

I am having issues sending emails to multiple recipients. The script is running well, creating files with the list of recipients from a table but not sending emails. I want to attach the file also. Here is a part of the script; please let me know the issue. Thank you.

if [ -s dataStnd_viol_addr.log ]; then

 if [ ${update_instance} == "BPROD"  ] 
  then
        ##########echo | mailx -s "***** Data Standard Violation - Address *****" -S 
      "[email protected]" -a "dataStnd_viol_addr.log" ${BPROD_LIST};
    while read dest;
        do 
            echo | mailx -s "***** Data Standard Violation - Address *****" -S 
      "[email protected]" $dest < $REPORT/dataStnd_viol_addr.log
        done < $REPORT/Email_PRODlist.lst
    elif [ ${update_instance} == "B8QA"  ]
    then
        while read dest1;
        do 
            echo | mailx -s "***** Data Standard Violation - Address *****" -S 
    "[email protected]" $dest1 < $REPORT/dataStnd_viol_addr.log
        done < $REPORT/Email_QAlist.lst
        
else 
    while read dest2;
        do 
            echo | mailx -s "***** Data Standard Violation - Address *****" -S 
  "[email protected]" $dest2 < $REPORT/dataStnd_viol_addr.log
            
        done < $REPORT/Email_DEVlist.lst 
fi

fi

cd ${working_dir}

working_dir=""; export working_dir
working_sql_dir=""; export working_sql_dir
update_instance=""; export update_instance
days_back=""; export days_back
1

There are 1 answers

3
glenn jackman On

You're providing input to mailx both with a pipe and with redirection. I don't know which takes precedence. I'd recommend removing all the echo| bits

Also, there seem to be 2 competing redirections, and mailx might be slurping up the input to the while loop.

Try this:

subject="***** Data Standard Violation - Address *****"
logfile="$REPORT/dataStnd_viol_addr.log"

while IFS= read -r dest2 <&3 ;do 
    mailx -s "$subject" -S "[email protected]" "$dest2" < "$logfile"
done 3< "$REPORT/Email_DEVlist.lst"

read is using file descriptor 3 and mailx is using file descriptor 1, no potential for conflict.

Note that this won't attach the file: the file contents will be the message body. Check if your version of mailx has a -a option.