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
linux email solaris aix mailx
add a comment |
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
linux email solaris aix mailx
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 twomailx
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
add a comment |
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
linux email solaris aix mailx
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
linux email solaris aix mailx
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 twomailx
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
add a comment |
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 twomailx
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Sep 10 at 11:21
Ivo Yordanov
214
214
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 twomailx
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