Search code examples
htmllinuxfontstclmime

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.


Solution

  • 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 test@example.com}]
    ::mime::setheader $tok Subject {Hello World}
    ::smtp::sendmessage $tok -servers smtp.example.com -recipients recipient@example.com -originator test@example.com
    ::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.