Reputation: 21
Can someone please explain how the below command works? Ignore the sendmail command switches as such - I know how that works. I want to know how the rest of the command works i.e. the here-document (without termination), cat reading from stdin and how it gets piped to sendmail.
SEND_MAIL()
{
`cat - $body <<HERE | /usr/lib/sendmail -oi -t
From: $SENDER
To: $RECEIVER
Subject: $SUBJECT
Content-Type: text/html; charset=us-ascii
MIME-Version: 1.0`
}
Update:
To answer some of the confusions, above code was written by someone else and surprisingly it does work. The author of the code simply executes the function and it successfully sends mail to the recipient with the content of $body.
Upvotes: 0
Views: 205
Reputation: 3671
So trick here is that -
is special to cat
. It makes cat
read "a file" from standard input. The here document writes the literal lines to cat
's standard input, so the effect is to concatenate the literal lines on the front of the file $body
.
Bash appears to accept a missing here-document delimiter within backticks, though it does whinge:
$ `cat <<HERE
echo foo
`
bash: warning: here-document at line 1 delimited by end-of-file (wanted `HERE')
foo
$
Those backticks aren't what you wanted, by the way. Try
SEND_MAIL()
{
cat - $body <<HERE | /usr/lib/sendmail -oi -t
From: $SENDER
To: $RECEIVER
Subject: $SUBJECT
Content-Type: text/html; charset=us-ascii
MIME-Version: 1.0
HERE
}
Upvotes: 1