Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write colored font in email using tcl

I automate a network switch using Tcl and expect scripts on my Fedora 12. The test logs and result with attachments are sent to an email inbox (office 365)-browser and outlook modes.

I would like to know if there is a way to make the color fonts appear in my email using TCL or shell script.

For example in the report sent to email, the text "Passed" should appear in Green-bold and the font "failed" must appear in Red-bold. Will tput be useful? Please help. Thanks in advance.

like image 871
code_trot Avatar asked Jan 29 '26 02:01

code_trot


2 Answers

Just use html email (with content-type: text/html header) and inline css to colorize it.

Passed should be

<span style="color:green"><font color="green"></font></span>

Here span provides styling
font provides fallback if span does not work. Some email clients may strip those inline styles.

like image 176
Shiplu Mokaddim Avatar answered Jan 30 '26 14:01

Shiplu Mokaddim


You are asking for two different things: color text in email and color text in the shell. The others already answer the email part, so I would like to address the shell part. For terminal output, I use the term::ansi::send package. Here is a sample:

package require cmdline
package require term::ansi::send

proc color_puts {args} {
    # Parse the command line args
    set options {
        {bg.arg default "The background color"}
        {fg.arg default "The foreground color"}
        {nonewline "" "no ending new line"}
        {channel.arg stdout "Which channel to write to"}
    }
    array set opt [cmdline::getoptions args $options]

    # Set the foreground/background colors
    ::term::ansi::send::sda_fg$opt(fg)
    ::term::ansi::send::sda_bg$opt(bg)

    # puts
    if {$opt(nonewline)} {
        puts -nonewline $opt(channel) [lindex $args end]
    } else {
        puts $opt(channel) [lindex $args end]
    }

    # Reset the foreground/background colors to default
    ::term::ansi::send::sda_fgdefault
    ::term::ansi::send::sda_bgdefault
}

#
# Test
#

puts "\n"
color_puts -nonewline -fg magenta "TEST"
color_puts -nonewline -fg blue    " RESULTS"
puts "\n"

color_puts -fg green "test_001 Up/down direction movements passed"
color_puts -fg red "test_002 Left/right direction movements failed"

Discussion

  • The applicable flags to color_puts are -bg for background color, -fg for foreground color, -nonewline for suppressing the new line character output, and -channel to direct the output to the file.
  • The available colors are black, blue, red, green, yellow, magenta, cyan, white, and default. For more information, look up the term::ansi::send package.
like image 39
Hai Vu Avatar answered Jan 30 '26 16:01

Hai Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!