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


22

我正在使用以下命令即时创建消息并发送:

echo "Subject:Hello \n\n I would like to buy a hamburger\n" | sendmail email@example.com

看起来,当您从文件发送信息时,可以执行以下操作:

sendmail email@example.com mail.txt 

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

如果要在sendmail中回显带有标头的消息,那么格式是什么?怎么做?

Answers:


24

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

echo -e "Subject:Hello \n\n I would like to buy a hamburger\n" | sendmail email@example.com

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

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"

1

Shell脚本中的“此处文档”(您组成消息标题和正文)

#!/bin/sh
TO=email@example.com
/usr/sbin/sendmail -i $TO <<MAIL_END
Subject: Hello
To: $TO

I would like to buy a hamburger
MAIL_END

来自外部文件的邮件正文

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

HEADERS_END
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.