BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
lydiawawa
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Sajid01
Meteorite | Level 14

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,

View solution in original post

8 REPLIES 8
Sajid01
Meteorite | Level 14

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.



lydiawawa
Lapis Lazuli | Level 10
Hi, I added the debugging feature and echo shell variables. There were no issues. On debug_output.txt, I can only echo before if [ $rc -gt 1 ]; , if I place it at the end, the echo code will not output. Still no email received.
Kurt_Bremser
Super User

Without being able to test anything (no UNIX available):

  • remove the semicolon after the if
  • use double square brackets ( if [[ $rc -gt 1 ]] )
  • where is your fi to terminate the if?
Sajid01
Meteorite | Level 14

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
lydiawawa
Lapis Lazuli | Level 10

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

Sajid01
Meteorite | Level 14

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,

lydiawawa
Lapis Lazuli | Level 10

It's some problem on my server. The code itself should be correct.

SASKiwi
PROC Star

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1739 views
  • 3 likes
  • 4 in conversation