1

LE. I killed everything html related. Declared 2 variables R and G (for red and green).

My mail shows the status [0;32mGLOBAL_STATUS = OK, obvious not in colours.

If I comment my variables I receive in the email GLOBAL_STATUS = OK -> no colours.

Pretty please help me receive the mail with green GLOBAL_STATUS = OK


I wrote this part of the code, but it doesn’t print in the email I should receive: GLOBAL_STATUS = OK in green ☹

It shows on top of the email: <h1> <span style='color:green;'> GLOBAL_STATUS = OK </span></h1><br/><br/><br/>

And at the end of the email: <html><body><br><br><br> </body> </html>

What am I doing wrong? I tried to eliminate the html, body, etc parts, but still, the beginning of the mail is the same: <h1> <span style='color:green;'> GLOBAL_STATUS = OK </span></h1><br/><br/><br/>

#echo "<html> ">>$LOG #echo "<body>" >> $LOG #echo "<br><br><br>" >> $LOG #echo "<br><br><br>" >> $LOG R='\033[0;31m' G='\033[0;32m' content="tmp.txt" global_status=0 while read line; do if [[ "$line" == *"KO"* && "$global_status" == "0" ]]; then echo -e "\n\n\n $line"; ((global_status=!global_status)); echo -e "\n $global_status" fi echo "$line" >> $content done < $LOG #echo "<h1>" > $LOG if [ "$global_status" -eq 0 ]; then echo -e "${G}GLOBAL_STATUS = OK" > $LOG else echo -e "${R}GLOBAL_STATUS = KO" > $LOG fi cat $content >> $LOG rm tmp.txt #echo "</body>" >> $LOG #echo "</html>" >> $LOG mail -s "Check Back" [email protected] < /home/check_back.log 
10
  • Welcome to the site. Pleas edit your post to specify how the generated e-mail is being sent to you. How do you ensure that the e-mail is sent in HTML format and not in plain-text format (which would treat all HTML commands as literal text, not formatting instructions)?
    – AdminBee
    CommentedAug 24, 2022 at 9:49
  • I edited the email part. The problem is that I need to check 17 servers status. And print if one KO, Global status KO. If all OK, Global Status ok. The servers status and info is super ok, just this snippet of code for GLOBAL STATUS is not working.
    – Lola
    CommentedAug 24, 2022 at 9:53
  • 1
    In seems you have a typo. The line echo "<h1>" > $LOG should read echo "<h1>" >> $LOG, otherwise you are overwriting everything written to the file so far.
    – AdminBee
    CommentedAug 24, 2022 at 9:57
  • if I put >> it doesn't send any status, only my servers information
    – Lola
    CommentedAug 24, 2022 at 9:58
  • you use $LOG both as mail body and status control (in while loop) ?
    – Archemar
    CommentedAug 24, 2022 at 10:02

2 Answers 2

1

Focusing on this:

Pretty please help me receive the mail with green GLOBAL_STATUS = OK

One problem you might have could be the Content-type header. If it marks the message as plaintext (text/plain), it'll likely show as such. You'll need to change the header to mark the type as text/html.

E.g. with the mail command from Debian's bsd-mailx package, this works at least as received by Gmail.

#!/bin/sh [email protected] mail -s "colortest" -a 'Content-Type: text/html; charset="UTF-8"' "$emailaddr" << EOF <html> <h1> <span style='color:green;'> GLOBAL_STATUS = OK </span></h1> </h1> <p> Blah blah... </p> </html> EOF 

At least this particular version of mail sets the content type to text/plain by default, unless overridden. See the man page for notes on -a and the MIME headers.

Note that other versions of the mail tool may be different, they may have a different way for setting headers. I also didn't check the HTML syntax at all: you may want to actually check the proper DOCTYPEs to use, etc. You might also need to check the encoding (Content-Transfer-Encoding header), if you do use non-ASCII characters in the message.

Also, to do it properly, you should probably generate a multipart MIME message with both text/plain and text/html variants. I'm not going to go there either.

As for that code, if you're only looking for the string KO from the input file, and passing the file through in full, you could just use grep instead of the shell loop. Maybe something in this direction (untested):

#!/bin/sh [email protected] status=XXX color=xxx subject=test inputfile=foo.txt msgfile=$(mktemp) if grep -qe KO "$inputfile"; then status='GLOBAL_STATUS = KO' color=red else status='GLOBAL_STATUS = OK' color=green fi > "$msgfile" cat <<EOF >> "$msgfile" <html> <h1><span style='color:$color;'> $status </span></h1> </h1> <pre> EOF cat "$inputfile" >> "$msgfile" cat <<EOF >> "$msgfile" </pre> </html> EOF mail -s "$subject" -a 'Content-Type: text/html; charset="UTF-8"' \ "$emailaddr" < "$msgfile" rm -f "$msgfile" 
3
  • Thank you so much for your time. I implemented your code, but still, I have no colours for the statuses. I also tried to put the colors with ' or ", same thing. Tried all caps, first letter big. Changed the email part as I had errors. ARG_EMAIL_TO="[email protected]" ARG_EMAIL_FROM="<[email protected]>" ARG_EMAIL_SUBJECT="Check BACK" ( echo "To: ${ARG_EMAIL_TO}" echo "From: ${ARG_EMAIL_FROM}" echo "Subject: ${ARG_EMAIL_SUBJECT}" echo "Mime-Version: 1.0" echo "Content-Type: text/html; charset='utf-8'" echo cat "$msgfile" ) | sendmail -t
    – Lola
    CommentedAug 25, 2022 at 10:01
  • @Lola, seems to work for me with that sendmail command too (and the message text from above). But I didn't test with other clients etc. I don't think I have any other ideas, sorry :/
    – ilkkachu
    CommentedAug 25, 2022 at 10:42
  • @ikkach I made it work. I saw a very strange thing, my last 15 minutes modification were ok. The fact is that from some strange thing all mails went to junk. I deleted my unlucky mails, and I don't know why, I went to deleted, and saw my mails had the green status. Went again trying to find what did I do that was good. Found it. Thank you so very much. :* >:D<
    – Lola
    CommentedAug 25, 2022 at 11:06
0

So my last step in this script is to compare the dates from the servers and compare them with today's date. If the date for one server is not today's date DATE = KO.

Tried this:

LOG="/home/check_back.log" date="date +%d-%m-%y -r" datemail=$(date "+%d-%m-%y") >LOG For each server the date is read and printed in the email with: echo " " && $date /tmp/check_back.log" >>$LOG if [[ $date -lt $datemail ]]; then check_date='DATE = KO' color='red' fi 

I receive the error: [[date +%d-%m-%y -r: command not found. I think my code doesn't read each servers date, but I have no idea what to do. Can someone help, please? Thanks in advance for the help.

1
  • Thanks all. I fixed it. I need to use < operator, not -lt.
    – Lola
    CommentedAug 26, 2022 at 17:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.