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..." [email protected]

Thanks!

5
  • 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". Commented Jan 26, 2016 at 14:10
  • Possible duplicate of Get color output in bash Commented Jan 26, 2016 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. Commented Jan 26, 2016 at 14:19
  • I am looking for green color in email "Job running...". I have edited the question please see above. Commented Jan 26, 2016 at 15:08
  • There is no way to color the email without using HTML... Commented Jan 27, 2016 at 6:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This does not email coloured text .
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.
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.