将消息传送到 sendmail 的格式是什么?

将消息传送到 sendmail 的格式是什么?

我使用以下命令动态创建消息并发送它们:

echo "Subject:Hello \n\n I would like to buy a hamburger\n" | sendmail [email protected]

似乎当您从文件发送信息时,可以通过执行以下操作:

sendmail [email protected] mail.txt 

然后 sendmail 将每一行视为标头,并对其进行解析。但按照我上面发送的方式,所有内容最终都在主题行中。

如果想将带有标头的消息回显到 sendmail 中,那么格式是什么?如何做到这一点?

答案1

您的语句实际上应该输出换行符,而不是后面跟着的echo序列。您可以通过提供以下选项来做到这一点:\n-e

echo -e "Subject:Hello \n\n I would like to buy a hamburger\n" | sendmail [email protected]

要了解有何区别,请查看以下两个命令的输出:

echo "Subject:Hello \n\n I would like to buy a hamburger\n"
echo -e "Subject:Hello \n\n I would like to buy a hamburger\n"

答案2

shell 脚本中的“此处文档”(您编写消息标题和正文)

#!/bin/sh
[email protected]
/usr/sbin/sendmail -i $TO <<MAIL_END
Subject: Hello
To: $TO

I would like to buy a hamburger
MAIL_END

来自外部文件的消息正文

#!/bin/sh
[email protected]
BODY_FILE=mail.txt
(cat - $BODY_FILE)<<HEADERS_END | /usr/sbin/sendmail -i $TO
Subject: Hello
To: $TO

HEADERS_END

相关内容