【问题标题】:How can I make contents of a txt file be used as subject for mail in linux?如何在 linux 中将 txt 文件的内容用作邮件的主题?
【发布时间】:2011-04-29 04:29:32
【问题描述】:

我有一个 crontab 文件,它执行如下所示的 shell 脚本

27 11 * * * /usr/python/bi_python/launcher/launch_script_leds.sh

shell 脚本做了很多事情:

1) 执行 python 脚本 launcher.py 运行测试并输出到日志文件

2) 测试完成后发送邮件通知,测试输出作为消息正文

这是 .sh 文件中的命令:

给我发邮件@sample.com/python/bi_python/launcher/test_output.txt

这工作正常,但主题是空白的

电子邮件的主题从launcher.py 输出到一个txt 文件subject.txt。有没有办法让这个文件的内容成为我邮件的主题?

我知道你可以使用 mail -s 来指定主题,但是由于许多测试是通过启动器运行的,所以主题总是会有所不同

提前致谢

【问题讨论】:

  • 你必须坚持使用shell发送邮件还是让python来做?

标签: python linux email cron


【解决方案1】:

试试

subject=$(</path/subject.txt)
mailx -s "$subject" me@sample.com < /usr/python/bi_python/launcher/test_output.txt

【讨论】:

  • 工作 非常感谢那个 ghostdog :) 我确信其他建议的解决方案也有效,但这是我尝试的第一个。谢谢大家
【解决方案2】:

好吧,只需将参数-s 传递给邮件命令,并带有合适的主题。

要将文件的内容用作主题,只需阅读文件即可。在 Bash 中,

filecontents=$(cat /my/file)

将/my/file 的内容读入变量filecontents。然后您可以根据需要截断/清理文本,并将其用作-s 的参数。

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2021-01-12
    相关资源
    最近更新 更多