Unix Shell Script does not send intended email - shell

I have a shell script like this. The purpose of this script is to tail+head out a certain amount of data from file.csv and then send it to email Bob#123.com. DataFunction seems to work fine alone however when I try to call DataFunction within the email function body. It seems it sends a empty email with the correct Title and destination. The body of the email is missing which should be the data from DataFunction. Is there a workaround for this ? Thank you in advance.
#!/bin/bash
DataFunction()
{
tail -10 /folder/"file.csv" | head -19
}
fnEmailFunction()
{
echo ${DataFunction}| mail -s Title Bob#123.com
}
fnEmailFunction

You are echoing an unset variable, $DataFunction (written ${DataFunction}), not invoking the function.
You should use:
DataFunction | mail -s Title Bob#123.com
You may have been trying to use:
echo $(DataFunction) | mail -s Title Bob#123.com
but that is misguided for several reasons. The primary problem is that it converts the 10 lines of output from the DataFunction function into a single line of input to mail. If you enclosed the $(DataFunction) in double quotes, that would preserve the 'shape' of the input, but it wastes time and energy compared to running the command (function) directly as shown.

Try this:
mail -s "Title" "bob#123com" <<EOF
${DataFunction}
EOF

I tried #0xAX's answer and couldnt get it to work properly within a bash script. You simply need to save the output of DataFunction() in some variable. You can simply use these three lines to achieve this.
#!/bin/bash
VAR1=`tail -10 "/folder/file.csv" | head -19`
echo $VAR1 | mail -s Title Bob#123.com

Related

Send email via echo command with data from variable

once a day a CSV is generated that includes a single column with several rows with names (one row for each name)
I want to send these names as an email to a certain distribution list.
This is what I have so far:
#!/bin/bash
names=`cat /list_of_names.csv`
echo "$names" | mail -s "You got mail" "email#email.com"
The problem is that the email is always empty. It is correctly delivered with the right subject line, but there is no text in it.
When I check the variable manually in the console (echo "$names") all names are listed correctly.
Why is the email empty? Does anyone have an idea?
Just in case you have problems reading the file
cat /list_of_names.csv 2>&1 | mail -s "You got mail" "email#email.com"

bash - loop and nested variable to store api response

I am learning to work with Bash and APIs, and am currently stuck on a problem related to nested variables.
The basic action I wish to do looks like this:
name="peter"; country="uk";
gender=$(http -b GET "https://api.genderize.io/?$name=peter&country_id=$uk" | jq -e '.gender');
echo $gender
output is
"male"
The complete action I want to take looks like this:
while read name <&3 && read country <&4
do gender=$(http -b GET "https://api.genderize.io/?name=$name&country_id=$country" | jq -e '.gender')
echo "$name : $gender" >> namesorted.txt
sleep 0.1
done 3<namelist.txt 4<countrylist.txt
but the output is null, which means that the request wasn't sent correctly:
Peter : null
Edouard : null
Henri : null
Anabelle : null
Nonso : null
Tom : null
What's wrong with my code? And is there a way for me to see the query sent by the code for debugging purposes?
edit: it ended up being a formatting mistake, as pointed out in the comments. Thanks!
As pointed in the comments, it ended up being a formatting mistake:
the use of double quotes in the initial script is correct, because it allows variables to be exanded:
gender=$(http -b GET "https://api.genderize.io/?$name=peter&country_id=$uk" | jq -e '.gender')
But in my full script I got mixed up with the single quotes, which kept the variables from being expanded. I fixed it in the original question, and the code works.
Thanks to #Gordon Davidsson for identifying the issue.

sending text file as email with curl when text file has variable

I have a script that sends an email with curl. For example:
name="John"
curl...
I have a text file for my email template. The template will have the $name variable in it. For example:
Hello, $name. This is a test.
The curl part is fine but the problem is that it sends Hello, $name. This is a test instead of Hello, John. This is a test. I'm not sure how to do this. I've tried to search how to do it but I'm not even sure how to phrase the question. I keep turning up stuff on reading variables values from a files which isn't what I'm looking to do.
This fixed my issue:
eval message=$(cat test.txt) | echo $message| curl --netrc-file "/config/.netrc" --ssl-reqd --mail-from "<myemail#gmail.com>" --mail-rcpt "<youremail#gmail.com>" --url smtps://smtp.gmail.com:465 -T -

Extra ! sign in email message body while sending mail through shell script

I am sending emails through shell script, in which I get message body from a text file. Issue comes when my disclaimer goes long, it starts getting extra ! sign in between.
Please suggest me to remove that without shortening or giving extra lines between the disclaimer. Let me know if you need any details.
Needed(say):
I am sending emails through shell script, in which I get message body from a text file. Issue comes when my disclaimer goes long, it starts getting extra ! sign in between.
Please suggest me to remove that without shortening or giving extra lines between the disclaimer. Let me know if you need any details.
Output in mail:
I am sending emails through shell script, in which I get message body from a text file. Issue comes when my disclaimer goes long, it starts getting extra ! sign in between.
Please suggest me to remove that without shortening or giving e!xtra lines between the disclaimer. Let me know if you need any details.
Here you can see that we are getting ! sign in extra word. Just keep the body long (sry I couldn't provide you the actual text), and you will get the ! sign.
Code I am using - to read from text file
EMAILMSG=$(cat $2) # $2 is path of text file
to send email
(echo -e $EMAILMSG;) | mailx -s "$SUBJECT" -b "abc#abc.com" $EMAIL -r $MAILBCC
I think this will help you to understand the situation. Please let me know anyone need further details.
I found this: Random exclamation mark in email body using CDO
It seems to indicate long lines are the problem. Try this using fold to wrap lines:
fold -s "$2" | mailx -s "$SUBJECT" -b "abc#abc.com" "$EMAIL" -r "$MAILBCC"
Style recommendations:
don't use $ALL_CAPS_VARS (here's why not)
always quote your "$vars" except when you know exactly when not to.

Showing special characters in bash echo

I am writing a bash script that reads the results of an sql query in which the results are output as HTML (using the -H option) to a file (using the -o option) and then sends those results in an email. When the results are output to the file, they come out as:
'<IDLE>'
But when I parse them from the output file they show up in the email as:
<IDLE>
Can anyone help me format these so I get the actual characters and not the entity representation?
EDIT: The way I am sending the text now is:
echo -e $EMAIL_TXT | mail -s $SUBJECT $RECIPIENT
And the way I am extracting the text from the html file ($OUT_FILE) is:
QRY_LINE=$(sed "${QRY_LNUM}q;d" $OUT_FILE)
I ended up just using string replace to replace all <s and >s with < and > respectively.

Resources