3

The following code works when I directly run it in bash shell:

SUBJECT="SUBJECT-"`date`;
MAIL_FROM="abc@site.com";
MAIL_TO="abc@site.com";
MAIL_CC="abc@site.com";
MAIL_FILE="/path/of/html/body.html";
(echo -e "Subject: $SUBJECT\nMIME-Version: 1.0\nFrom: $MAIL_FROM\nTo:$MAIL_TO\nCc:$MAIL_CC\nContent-Type: text/html\nContent-Disposition: inline\n\n";/bin/cat $MAIL_FILE) | /usr/sbin/sendmail -f  $MAIL_FROM $MAIL_TO;

But when I try to run it within a script like below...

Contents of mail.sh:

#!/usr/bin/ksh

SUBJECT="SUBJECT-"`date`;
MAIL_FROM="abc@site.com";
MAIL_TO="abc@site.com";
MAIL_CC="abc@site.com";
MAIL_FILE="/path/of/html/body.html";
(echo -e "Subject: $SUBJECT\nMIME-Version: 1.0\nFrom: $MAIL_FROM\nTo:$MAIL_TO\nCc:$MAIL_CC\nContent-Type: text/html\nContent-Disposition: inline\n\n";/bin/cat $MAIL_FILE) | /usr/sbin/sendmail -f  $MAIL_FROM $MAIL_TO;

I get the following results...

$ sh mail.sh #Mail sent but the body is in text format containing "-e Subject: SUBJECT-Wed Jan 30 04:45:42 EST....." and the HTML code rendered as text.

$ bash mail.sh # Mail is received with mail body containing correct HTML format.

So it seems that echo -e is recognized by bash. But when I use "#!/usr/bin/bash" and run the script as $ sh mail.sh I still get a mail in text format.

Why is this so..? Thanks in advance for your suggestions.

3

The Solaris echo

$ echo -e foo
-e foo

does not work like most some other echo commands:

$ bash
$ echo -e foo
foo 
$ which echo
/usr/bin/echo
$ type -t echo
builtin

The bash builtin version works as expected, the ksh builtin keeps the Solaris behaviour (echo behaviour is usually system dependent in ksh when options are used). A plain echo should work in ksh, no -e:

$ ksh
$ echo -e "foo\nbar"
-e foo
bar
$ echo "foo\nbar"
foo
bar

So you have a Solaris problem, rather than a sendmail problem :-)

You could try printf as a more portable way of doing this.

  • Okay.. Wow it would have taken me a while to figure that out. Will try it with printf instead. Thanks! – Kent Pawar Jan 30 '13 at 11:20
  • 2
    The -e is a GNU extension. – vonbrand Jan 30 '13 at 11:39

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.