I have the following shell script fragment.

for emailadd in `cat /tmp/email_list.tmp`
   subject_text="Subject with Var Name "$var_name
   subj_text_novar="Subject without Var Name"
   email_mssge="this is the message with variable name "$var_name
   echo "$email_mssge"|mailx -a /tmp/my_report.txt -s "$subject_text" "$emailadd"
   echo "$email_mssge"|mailx -a /tmp/my_report.txt -s "$subj_text_novar" "$emailadd"

What it does is a. Sets the variable var_name b. Reads through the list of email addresses stored in /tmp/email_list.tmp c. Compiles and sends an email with /tmp/my_report.txt (plain text file) as an attachment

The script is intended to run on a number of different servers, so var_name will change with each server. The mail command with $subj_text_novar, (does not include $var_name in the subject string), sends the email correctly . However the mail command with $subject_text which does include $var_name places the contents of the attachment into the main body of the email. As far as I can make out, it is the actual $-sign causing the problem because hardcoding the var_name value into the string is fine but I don't see why because surely var_name is just a concatenated string So, how can I set the subject for mail so it includes $var_name and my text file is sent as an attachment?

I am running this on SuSE 11.3 but the solution ideally needs to work on AIX 6.1 and HP UX11.31 as well


  • Quote the variables. For example, subject_text="Subject with Var Name $var_name". Do you need to send a separate message to each recipient in /tmp/email_list.tmp? If not, you can discard the loop and put $(cat /tmp/email_list.tmp) in place of "$emailadd" on the two mailx command lines – roaima Jul 21 '15 at 15:32
  • beware: the -a flag to mailx may not exist on your AIX and/or HPUX machines. – Jeff Schaller Jul 21 '15 at 15:37
  • Thanks to roaima for the answer. In fact I had tried this before without joy. But roaima's comment made me re-evaluate and in the context of the shell fragment yes it worked. However I was actually deriving var_name from an Oracle database, (probably I should've mentioned it but to be honest I didn't think that was the issue). Anyway by doing var_name=echo $dbs_value I was able to employ roaima's solution. – Noj Jul 21 '15 at 16:02
  • And thanks Jeff Schaller about AIX & HP. I'll investigate both OS's to see if there are alternatives – Noj Jul 21 '15 at 16:06

I have something for you using heirloom-mailx:

enviaremail() {
values=$(echo "$@" | tr -d '\n')
heirloom-mailx $( attachment=""
for (( a = 5; a < ${#listargs[@]}; a++ )); do
attachment=$(echo "-a ${listargs[a]} ")
echo "${attachment}"
done) -v -s "${titulo}" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://$1 \
-S from="${2}" \
-S smtp-auth-user=$3 \
-S smtp-auth-password=$4 \
-S ssl-verify=ignore \
$5 < ${cuerpo}

function call: enviaremail smtp.mailserver:port from_address authuser 'pass' destination list of attachments separated by space

In addition please remember to define externally the $titulo (subject) and $cuerpo (body) of the email prior to using the function. You can put the function as a script (mailsend.sh) in the path and then just use it in your scripts with source or place it in your .bashrc file.

Best Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.