0

How do I put color coding in Linux for email for these 2 scenarios? I have looked at the other stack, they are building functions, is there any simple way to do this (without HTML) coding?

1) Job running... text in green color?
echo -e "Job running..." | mail -s "Job running..." 

2) Job failing... text in red color?
echo -e "Job failing..." | mail -s "Job failing..." 

Tries. echo gives on the front end, but doesn't send the color in email.
echo -e '\033[0;32m'"\033[1mJob running...\033[0m" | mail -s "Job running..." test@example.com

Thanks!

sharp
  • 2,140
  • 9
  • 43
  • 80
  • Were you asking for ansi colors or HTML? In either case you need to provide the email reader with enough information to colorize your message. It doesn't happen by default as emails are "just text". – AlG Jan 26 '16 at 14:10
  • Possible duplicate of [Get color output in bash](http://stackoverflow.com/questions/2437976/get-color-output-in-bash) – NaN Jan 26 '16 at 14:15
  • If you are expecting the recipients of the email to see the text colours, you should be composing an HTML message. You'll have to set the Content-Type header, so you're beyond a simple pipe to `mail`. – glenn jackman Jan 26 '16 at 14:19
  • I am looking for green color in email "Job running...". I have edited the question please see above. – sharp Jan 26 '16 at 15:08
  • There is no way to color the email without using HTML... – Nidhoegger Jan 27 '16 at 06:11

1 Answers1

1

Use the ANSI Escape Sequences (if using bash!):

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

echo -e "${GREEN}Job running...${NC}" | mail -s "Job running..." 
echo -e "${RED}Job failing...${NC}" | mail -s "Job failing..." 

Coloring the mail for the reciepient without using HTML is not possible. If you wish for color in the resulting mail use HTML formatting.

Nidhoegger
  • 4,973
  • 4
  • 36
  • 81
  • This does not email coloured text . – pythonRcpp Sep 28 '17 at 07:00
  • You cannot color text in mails without HTML! You can insert the ANSI Sequences in the Mailtext, then the mail will be colored when viewing the mail on command line. But not when viewing e.g. with Thunderbird. – Nidhoegger Oct 02 '17 at 09:46
  • ok, please mention that in the answer. Mostly it conveys that you are talking about email seen in GUI (browser) and not terminal. (removing my downvote :) – pythonRcpp Oct 03 '17 at 06:14