code_trot
code_trot

Reputation: 87

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.

Upvotes: 3

Views: 1318

Answers (3)

Hai Vu
Hai Vu

Reputation: 40763

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.

Upvotes: 3

Johannes Kuhn
Johannes Kuhn

Reputation: 15173

So, here is a simple script that I use to send mails (you might need to provide a username/password for smtp::sendmessage)

set textpart [::mime::initialize -canonical text/plain -string {Hello World}]
set htmlpart [::mime::initialize -canonical text/html -string  {<font color="green">Hello World</font>}]
set tok [::mime::initialize -canonical multipart/alternative -parts [list $textpart $htmlpart] -header {From [email protected]}]
::mime::setheader $tok Subject {Hello World}
::smtp::sendmessage $tok -servers smtp.example.com -recipients [email protected] -originator [email protected]
::mime::finalize $tok -subordinates all

Some notes:

  • You can use different messages for html and plain text, but you should include all information in both. The client usually picks the better format that it can display.
  • If you want to send attachments, you have to add an other multipart/mixed, (build it like the multipart/alternative), the first part of it should be the message (your multipart/alternative) the other parts are the attachments.
  • Depending on some more or less obscure circumstances, the smtp and the mime package use some invalid system defaults (like your username with a space). If this happens, you have to provide extra information to one or more of this commands.

Upvotes: 2

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57670

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.

Upvotes: 3

Related Questions