我想在bash
中使用sendmail命令发送电子邮件。电子邮件应该通过阅读Input_file_HTML
来获取它的身体,它也应该发送相同的输入文件作为附件。为此,我尝试了以下方法:
sendmail_touser() {
cat - ${Input_file_HTML} << EOF | /usr/sbin/sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: $1
Content-Type: text/html; charset=us-ascii
cat ${Input_file_HTML}
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Content-Disposition: attachment; filename: ${Input_file_HTML}
EOF
}
上述命令只会发送一封只包含Input_file_HTML
附件的电子邮件,而不会将其写入电子邮件正文中。你能帮我/指导一下吗?我使用outlook作为电子邮件客户端。我甚至在上面的命令中删除了cat
命令,但它也无效。
答案 0 :(得分:3)
改为使用mutt
?
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com
在Debian系统上安装mutt
:
sudo apt-get install -y mutt
编辑如果您只能使用sendmail
:
sendmail_attachment() {
FROM="$1"
TO="$2"
SUBJECT="$3"
FILEPATH="$4"
CONTENTTYPE="$5"
(
echo "From: $FROM"
echo "To: $TO"
echo "MIME-Version: 1.0"
echo "Subject: $SUBJECT"
echo 'Content-Type: multipart/mixed; boundary="GvXjxJ+pjyke8COw"'
echo ""
echo "--GvXjxJ+pjyke8COw"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
echo "<p>Message contents</p>"
echo ""
echo "--GvXjxJ+pjyke8COw"
echo "Content-Type: $CONTENTTYPE"
echo "Content-Disposition: attachment; filename=$(basename $FILEPATH)"
echo ""
cat $FILEPATH
echo ""
) | /usr/sbin/sendmail -t
}
像这样使用:
sendmail_attachment "to@example.com" "from@example.com" "Email subject" "/home/user/file.txt" "text/plain"