我在我的Fedora 12上使用Tcl和expect脚本自动化了一个网络交换机。测试日志和带有附件的结果被发送到电子邮件收件箱(office 365)-浏览器和outlook模式。
我想知道是否有一种方法,使颜色字体出现在我的电子邮件使用TCL或shell脚本。
例如,在发送到电子邮件的报告中,文本"通过"应该以绿色粗体显示,字体"失败"必须以红色粗体显示。输出有用吗?请帮助。
只需使用html电子邮件(与content-type: text/html
标题)和内联css来着色。
Passed
应为
<span style="color:green"><font color="green"></font></span>
这里span
提供样式
如果span不工作,font
提供回退。一些电子邮件客户端可能会剥离这些内联样式。
您要求的是两种不同的东西:电子邮件中的彩色文本和shell中的彩色文本。其他人已经回答了邮件部分,所以我想解决shell部分。对于终端输出,我使用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
一些注意事项:
- 你可以对html和纯文本使用不同的消息,但是你应该在两者中包含所有的信息。客户端通常选择它可以显示的更好的格式。
- 如果你想发送附件,你必须添加另一个
multipart/mixed
,(像multipart/alternative
一样构建它),它的第一部分应该是消息(你的multipart/alternative
),其他部分是附件。 - 根据一些或多或少模糊的情况,smtp和mime包使用一些无效的系统默认值(如您的用户名带有空格)。如果发生这种情况,您必须为一个或多个命令提供额外的信息。