2

Below script works fine in my Linux server.

(
echo "From: name@xyz.com";
echo "To: name2@xyz.com";
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: name@xyz.com";
echo "To: name2@xyz.com";
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
1

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: name2@xyz.com;"
echo "From: name@xyz.com;"
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) foo@exemple.com
0

Remove the echo commands from the variable definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.