Hi,
I'm using the following code to send an email when an SAS session finishes successfully or fails midway. For some reason, I never receive the emails.
${sasroot}/sas -autoexec $autoexec -memsize 8G -sortsize 4G -cpucount 4 \
-sysin $sascoderoot/$progname.sas \
-log $projlog/${progname}_${logtimestamp}.log \
-print $projout/${progname}_${logtimestamp}_${sector}.lst \
-noterminal -nodms -errorabend; rc=$?
echo $?
# For SAS: 0=ok, 1=warn, >1=error.
if [ $rc -gt 1 ];
then
subject="$sector $jobdesc process was unsuccessful"
msg="$sascoderoot/$progname.sas sector $sector had errors (SAS rc=$rc). Please Review the log ($projlog/${progname}_${logtimestamp}.log). Please do not reply to sender."
echo -e "$msg" | mail -s "$subject" -r "$sender" "$failemail"
exit $rc
else
subject="$sector $jobdesc process was successful"
msg="$sascoderoot/$progname.sas $sector sector ran successfully. Please do not reply to sender."
echo -e "$msg" | mail -s "$subject" -r "$sender" "$supportemail"
autoexe, sysin, log and lst are all successfully executed. `$sender`, `$failemail` and `
$supportemail` are all defined. What could be the possible cause?
The script I gave works. My presumption is that you are running Linux .
Please test to see only the portion of the code I gave on your system. In place of info@example.com have your email address.
If that does not work then try this
echo -e "Hello" | mailx -s "Test" -r "Your email id" "email id"
If that does not work, then what is your OS and what shell you are using. (echo ${SHELL} shold give info about your shell).
Do approach your OS admin for assistance,
Looks like you are running a shell script. Assuming it to be a bash script.
I suggest the following.
1. Add this to the top of the script and capture the output to another file. set -x Thus if your script is send_mail.sh then for debugging run ./send_mail.sh > debug_output.txt 2.Echo all shell variable to see if they are assigned proper values. Thus you musg echo $msg, $subject $sender etc 3.Unless you are using escaped characters the consider removing -e after echo in echo - $msg | ... 4.I prefer enclosing shell variable in curly braces. Thus ${msg} in place of $msg, ${subject} in place of $subject. .5.Read the debug_output.txt and you may get an idea of whats going on.
Without being able to test anything (no UNIX available):
Hello I have modified the operative portion of your email script.
This works on bash shell on Red Hat / Cent. Adjust to your script.
Reply if you are having issues
#!/bin/bash set -x ##Variable definitions SECTOR=10 JOBDESC=some_job PROJLOG=projlog PROGNAME=program SAS_CODE_ROOT="your_root_path" LOG_TIME_STAMP=`date +%Y-%m-%d_%H_%M_%S` SENDER="info@example.com" SUPPORT_EMAIL="info@example.com" FAIL_EMAIL="info@example.com" RETURN_CODE=0; #### Send emai1#### if [[ ${RETURN CODE} -gt 1 ]]; then SUBJECT="${SECTOR}+ ${JOBDESC} process was unsuccessful" MSG= "${SAS_CODE_ROOT}/${PROGNAME}.sas SECTOR ${SECTOR had errors (SAS Return Code is ${RETURN CODE}). Please Review the log (/${PROJLOG}/${PROGNAME}_${LOG_TIME_STAMP}.log). Please do not reply to SENDER echo ${MSG}" echo -e "${MSG}" | mailx -s "${SUBJECT}" -r "${SENDER}" "${FAIL_EMAIL}" exit ${RETURN CODE} else SUBJECT="${SECTOR} ${JOBDESC) process was successful" MSG="${SAS_CODE_ROOT}/${PROGNAME}.sas ${SECTOR} SECTOR ran successfully. Please do not reply to SENDER" echo -e "${MSG}" | mailx -s "${SUBJECT}" -r "${SENDER]" "${SUPPORT_EMAIL}" fi
I applied changes suggested by both responses (fi was present in the program, I made a typo when I made this post). There is still no email.
${sasroot}/sas -autoexec ${autoexec} -memsize 8G -sortsize 4G -cpucount 4 \
-sysin ${sascoderoot}/${progname}.sas \
-log ${projlog}/${progname}_${logtimestamp}.log \
-print ${projout}/${progname}_${logtimestamp}_${sector}.lst \
-noterminal -nodms -errorabend; rc=$?
echo $?
# For SAS: 0=ok, 1=warn, >1=error.
if [[ ${rc} -gt 1 ]]
then
subject="${sector}+ ${jobdesc} process was unsuccessful"
msg="${sascoderoot}/${progname}.sas sector ${sector} had errors (SAS rc=${rc}). Please Review the log (/${projlog}/${progname}_${logtimestamp}.log). Please do not reply to sender."
echo -e "${msg}" | mailx -s "${subject}" -r "${sender}" "${failemail}"
exit ${rc}
else
subject="${sector}+ ${jobdesc} process was successful"
msg="${sascoderoot}/${progname}.sas ${sector} sector ran successfully. Please do not reply to sender."
echo -e "${msg}" | mailx -s "${subject}" -r "${sender}" "${supportemail}"
fi
The script I gave works. My presumption is that you are running Linux .
Please test to see only the portion of the code I gave on your system. In place of info@example.com have your email address.
If that does not work then try this
echo -e "Hello" | mailx -s "Test" -r "Your email id" "email id"
If that does not work, then what is your OS and what shell you are using. (echo ${SHELL} shold give info about your shell).
Do approach your OS admin for assistance,
It's some problem on my server. The code itself should be correct.
Have you tried the mailx command by typing in the correct details at an OS command line? If it doesn't work from a command line it surely wont work in a script. If that's the case then get help from your IT administrator to get it working.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.