3

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

SUBJECT="SUBJECT-"`date`;
MAIL_FROM="[email protected]";
MAIL_TO="[email protected]";
MAIL_CC="[email protected]";
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="[email protected]";
MAIL_TO="[email protected]";
MAIL_CC="[email protected]";
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.

1 Answer 1

4

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.

3
  • Okay.. Wow it would have taken me a while to figure that out. Will try it with printf instead. Thanks!
    – Kent Pawar
    Commented Jan 30, 2013 at 11:20
  • 2
    The -e is a GNU extension.
    – vonbrand
    Commented Jan 30, 2013 at 11:39
  • unix.stackexchange.com/q/65803/5132 is relevant here.
    – JdeBP
    Commented Dec 31, 2019 at 18:19

You must log in to answer this question.

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