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 -
Related
I have data phone in phone.txt
+6285712341234
+6285712341235
+6285712341236
+6285712341237
+6285712341238
but I don't know how to use this data to curl, here's what I tried:
curl -X POST "https://rest-api.moceansms.com/rest/1/sms" -d "mocean-api-key={api_key}&mocean-api-secret={api_secret}&mocean-from={name}&mocean-to={phone.txt}&mocean-text=Hello"
I should use phone data to send SMS to everyone; I googled for a solution, but with no luck (I don't even know whether the keywords I used to look for a solution where correct or not).
You should use --data flag:
Check:
https://curl.haxx.se/mail/archive-2007-03/0097.html
https://curl.haxx.se/docs/manpage.html#-d
Here the entire explanation from man:
-d, --data
(HTTP) Sends the specified data in a POST request to the HTTP server,
in the same way that a browser does when a user has filled in an HTML
form and presses the submit button. This will cause curl to pass the
data to the server using the content-type
application/x-www-form-urlencoded. Compare to -F, --form.
--data-raw is almost the same but does not have a special interpretation of the # character. To post data purely binary, you
should instead use the --data-binary option. To URL-encode the value
of a form field you may use --data-urlencode.
If any of these options is used more than once on the same command
line, the data pieces specified will be merged together with a
separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would
generate a post chunk that looks like 'name=daniel&skill=lousy'.
If you start the data with the letter #, the rest should be a file
name to read the data from, or - if you want curl to read the data
from stdin. Multiple files can also be specified. Posting data from a
file named 'foobar' would thus be done with -d, --data #foobar. When
--data is told to read from a file like that, carriage returns and newlines will be stripped out. If you don't want the # character to
have a special interpretation use --data-raw instead.
See also --data-binary and --data-urlencode and --data-raw. This
option overrides -F, --form and -I, --head and --upload.
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
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.
I have a nice bash script which uses curl to log on to a site and initiate an action which all works well.
The output has some noise in it which I would like to remove. The redirect for the authentication page (which works) generates the following message:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>
I really just want to turn off the generation of this message but can't see how to.
It looks like you might have figured out your own question, but if not you can do this:
curl --silent --fail
"--fail or -f" will not show any HTTP output errors on servers and "--silent and -s" should make it completely quiet, however, usually when you get to an "object moved" you can use "-L" for automatic redirection to avoid that output.
I hope this answered your question and for more information: http://curl.haxx.se/docs/manpage.html or man curl
In a comment, you said "I'd rather not redirect std output as I want to see genuine errors." If it was me, I'd capture stdout, check if it was an error response, and if so, display it.
E.g.,
rslt=$( curl ... )
stat=${rslt##*\<rsp stat=\"}
stat=${stat%%\"\>*}
if [[ "$stat" == "fail" ]]; then
# parse: <err code="4" msg="your bad" />
code=${rslt##*\<err code=\"}
code=${code%%\"*}
msg=${rslt##*msg=\"}
msg=${msg%%\"*}
fi
echo "curl failed: $stat: ($code): $msg"
Of course, doing this is very dependent on the format of the HTML response, so it will break if the server changes the response format.
Suppose I have a bash script that goes through a file that contains a list of old URLs that have all been redirected.
curl --location http://destination.com will process a page by following a redirect. However, I'm interested not in the content, but on where the redirect points so that I can update my records.
What is the command-line option for curl to output what that new location for the URL is?
You wou want to leave out the --location/-L flag, and use -w, checking the redirect_url variable. curl -w "%{redirect_url}" http://someurl.com should do it.
Used in a script:
REDIRECT=`curl -w "%{redirect_url}" http://someurl.com`
echo "http://someurl.com redirects to: ${REDIRECT}"
From the curl man page:
-w, --write-out <format>
Make curl display information on stdout after a completed transfer. The
format is a string that may contain plain text mixed with any number
of variables. The format can be specified as a literal "string", or
you can have curl read the format from a file with "#filename" and to
tell curl to read the format from stdin you write "#-".
The variables present in the output format will be substituted by the
value or text that curl thinks fit, as described below. All variables
are specified as %{variable_name} and to output a normal % you just
write them as %%. You can output a newline by using \n, a carriage
return with \r and a tab space with \t.
NOTE: The %-symbol is a special symbol in the win32-environment, where
all occurrences of % must be doubled when using this option.
The variables available are:
...
redirect_url When an HTTP request was made without -L to follow
redirects, this variable will show the actual URL a redirect would
take you to. (Added in 7.18.2)
...
This might work (as a starting point)
curl -sI google.com | head -1 | grep 301 | wc -l
man curl
then
search redirect_url
redirect_url When a HTTP request was made without -L to follow
redirects, this variable will show the actual URL a redirect would
take you to. (Added
in 7.18.2)
the variable above is for -w/--write-out <format>