0

I'm trying to setup a email blast using curl in a shell script.

The problem I'm facing is that I have to setup a function just so I can use $f2 but cannot get the function to work in the curl loop.

I tried doing "echo" in the fuction and also removing the $ from emailtext but still nothing.

It justs outputs a blank email.

function emailtext() { "Dear $f2, Testing some Mailgun awesomeness!" }

paste $emaillist $namelist | while IFS="$(printf '\t')" read -r f1 f2
do
curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
    -F to=$f1 \
    -F subject='Hello' \
    -F text='$emailtext'
done
  • `IFS=$'\t'` is much more efficient than `IFS=$(printf '\t')`. And BTW, didn't I show you an approach to avoid `paste` altogether in a prior question surrounding the same code? – Charles Duffy Sep 22 '18 at 01:50
  • ...hmm, maybe not you. Where'd you get this code from, then? The other person had the same variable names, the same silly `printf` idiom, etc. – Charles Duffy Sep 22 '18 at 01:50
  • ...ahh, it was https://stackoverflow.com/questions/52435330/expanding-a-string-with-a-variable-reference-later-after-the-variable-is-assign/52435403 -- see the other generalized improvements suggested there. Also, run everything through http://shellcheck.net/ and fix the bugs it identifies; in particular, you're missing a lot of quotes. – Charles Duffy Sep 22 '18 at 01:51
  • 1
    BTW, also see http://wiki.bash-hackers.org/scripting/obsolete re: function declaration syntax -- it's better form to write `emailtext() {` with no preceding `function` to be compatible with shells implementing the POSIX sh standard; and if you **do** insist on the `function`, then leave out the `()` to be compatible with legacy ksh (compatibility with pre-POSIX ksh is why bash permits the `function` keyword in the first place). Right now, you're compatible with neither. – Charles Duffy Sep 22 '18 at 01:52

1 Answers1

0

Functions are invoked like commands, not like variables. Thus:

-F text="$(emailtext)"

Note, too, that functions contain commands. Your current function definition isn't going to do anything more than throw a command not found error.


It would be better form to pass arguments to your function explicitly rather than relying on shared variables:

emailtext() { echo "Dear $1, testing some awesomeness"; }

curl ... -F text="$(emailtext "$f2")"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441