Shell

將消息傳送到 sendmail 的格式是什麼?

  • December 20, 2021

我正在使用以下命令即時創建消息並發送它們:

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 中,那麼格式是什麼?如何做到這一點?

您的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"

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

引用自:https://unix.stackexchange.com/questions/168232