0

I use shell script as below to get the string partly bold which works and results in the correct string echo'd in the terminal:

bold=$(tput bold)
normal=$(tput sgr0)
str="${bold}ERROR${normal} detail text"
echo -e "$str"

It does not work together with mail command:

echo -e "$str" | mail -s "daily notification" 123@abc.com

I receive an email with "[1mERROR", But not "ERROR", anyone know why?

Jongware
  • 22,200
  • 8
  • 54
  • 100
zhihong
  • 1,808
  • 2
  • 24
  • 34
  • Because the output from `tput` is the control codes necessary to get your current terminal to display in bold and that doesn't make any sense to put in an email's contents. – Etan Reisner Nov 16 '15 at 16:22
  • @EtanReisner, thanks for the quick answer. OK, that makes sense. Then is it possible to add the format in the email? – zhihong Nov 16 '15 at 16:30
  • Yes, use HTML email and HTML formatting. – Etan Reisner Nov 16 '15 at 16:35

1 Answers1

4

There is no such concept as "bold text" in plain ASCII (aka text/plain), which is what traditional clients such as mail and its close relative mailx send. If you compose text/enriched (which never really took off) or another structured format like text/html, text/rtf etc then the facility is available, but the formatting codes to achieve it will most certainly not be the terminal control sequences you produce with tput. These days, the simplest approach is probably to compose your message in HTML, and use a client which can properly set the Content-Type: header to reflect this. (Some variants of mailx apparently have this, but there are many incompatible ones which don't.)

So the following might work (source: https://stackoverflow.com/a/27693507/874188), if you're lucky and have a compatible mailx version:

echo '<html><body><p>Test <b>bold</b> text</p></body></html>' |
mailx -a 'Content-Type: text/html' -s "Daily notification" 123@example.com

It would be nice if there was a universal formatting language which adapted and scaled to the facilities and conventions for terminals, print, and graphical displays in various form factors; at various times, Unix roff, TeX, PostScript (which is still at the heart of PDF) and Microsoft's RTF have showed some promise, but none of them quite made it. Currently, HTML is the strongest contender in this field in my humble estimation, but still not quite there.

Meanwhile, some modern graphical email clients will display *bold* as bold and _italics_ as italics even in plain-text messages; and even users who don't see this graphical markup can usually understand and appreciate these conventions.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318