我使用Tcl和expect脚本自动化Fedora 12上的网络交换机。测试日志和附件结果发送到电子邮件收件箱(Office 365)-浏览器和Outlook模式。我想知道是否有一种方法可以使用TCL或shell脚本使彩色字体在我的电子邮件中出现。例如,在发送到电子邮件中的报告中,“通过”文本应以绿色粗体显示,而“失败”字体必须以红色粗体显示。tput是否有用?请帮忙解决。谢谢。
只需使用HTML电子邮件(带有content-type:text/html
标头)和内联CSS对其进行着色。
Passed
应该是
<span style="color:green"><font color="green"></font></span>
这里的span
提供了样式。font
则作为备用,如果无法生效。有些电子邮件客户端可能会删除这些内联样式。
term::ansi::send
包。下面是一个示例: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"
color_puts
可用的标志包括 -bg
表示背景颜色,-fg
表示前景颜色,-nonewline
表示抑制新行字符输出,-channel
表示将输出定向到文件。term::ansi::send
包。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
multipart/mixed
(构建方式与multipart/alternative
相似),其中的第一部分应该是消息(您的multipart/alternative
),其他部分是附件。