2

Below script works fine in my Linux server.

(
echo "From: [email protected]";
echo "To: [email protected]";
echo "Subject: Test Subject";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "<html><head><title>HTML E-mail</title></head>
<body><p>Test Body Message</p></body></html>";
) | sendmail -t

But after modify the script as below, it shows error as "No recipient address found in header". Is it possible to workout the below code using variable. I don't want to use any file or any other mail command except sendmail.

var1='(
echo "From: [email protected]";
echo "To: [email protected]";
echo "Subject: Test Subject";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "<html><head><title>HTML E-mail</title></head>
<body><p>Test Body Message</p></body></html>";
)'

echo ${var1} | sendmail -t

2 Answers 2

2

command substitution will replace end of line by space.

  • use ;" instead of ";
  • use tr \; '\n' to renable end of line.
  • use awk to de-indent first char as space
  • put To: line first

this result in somehow ugly

var1=$(
echo "To: [email protected];"
echo "From: [email protected];"
echo "Subject: mail HTML ;"
echo "Content-Type: text/html;"
echo "MIME-Version: 1.0;"
echo ";"
echo "<html><head><title>HTML E-mail</title></head>
body><p>Test Body Message</p></body></html>"
)

## can be one-lined, pipe symbol (|) continue line.
echo ${var1} | tr \; '\n' |
awk 'length == 1 || NR == 1 {print $0} length && NR > 1 { print substr($0,2) }'  |
sendmail -t

this however my break some html code ( like &aacute; turn to &aacute ), you know that mail can send attachment now days ?

man mail
   -A, --attach=FILE
          attach FILE
mail -A <(generate HTML result) [email protected]
0

Remove the echo commands from the variable definition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .