How to set mail subject using variable and ensure attachment is not included in email body











up vote
0
down vote

favorite












I have the following shell script fragment.



var_name='ZZPCI'
for emailadd in `cat /tmp/email_list.tmp`
do
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"
done


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



Regards










share|improve this question
























  • 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















up vote
0
down vote

favorite












I have the following shell script fragment.



var_name='ZZPCI'
for emailadd in `cat /tmp/email_list.tmp`
do
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"
done


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



Regards










share|improve this question
























  • 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













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following shell script fragment.



var_name='ZZPCI'
for emailadd in `cat /tmp/email_list.tmp`
do
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"
done


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



Regards










share|improve this question















I have the following shell script fragment.



var_name='ZZPCI'
for emailadd in `cat /tmp/email_list.tmp`
do
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"
done


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



Regards







linux email solaris aix mailx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 21 '15 at 15:43









roaima

42.2k550115




42.2k550115










asked Jul 21 '15 at 15:28









Noj

111




111












  • 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


















  • 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
















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






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




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






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




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










1 Answer
1






active

oldest

votes

















up vote
0
down vote













I have something for you using heirloom-mailx:



enviaremail() {
values=$(echo "$@" | tr -d 'n')
listargs=()
listargs+=($values)
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






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f217402%2fhow-to-set-mail-subject-using-variable-and-ensure-attachment-is-not-included-in%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I have something for you using heirloom-mailx:



    enviaremail() {
    values=$(echo "$@" | tr -d 'n')
    listargs=()
    listargs+=($values)
    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






    share|improve this answer

























      up vote
      0
      down vote













      I have something for you using heirloom-mailx:



      enviaremail() {
      values=$(echo "$@" | tr -d 'n')
      listargs=()
      listargs+=($values)
      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






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I have something for you using heirloom-mailx:



        enviaremail() {
        values=$(echo "$@" | tr -d 'n')
        listargs=()
        listargs+=($values)
        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






        share|improve this answer












        I have something for you using heirloom-mailx:



        enviaremail() {
        values=$(echo "$@" | tr -d 'n')
        listargs=()
        listargs+=($values)
        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 10 at 11:21









        Ivo Yordanov

        214




        214






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f217402%2fhow-to-set-mail-subject-using-variable-and-ensure-attachment-is-not-included-in%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt