Sendmail 命令将文件作为电子邮件正文和附件发送

Sendmail command to send a file as an email body as well as attachment

我想在 bash 中使用 sendmail 命令发送电子邮件。电子邮件应该通过阅读 Input_file_HTML 来获取正文,并且它也应该将相同的输入文件作为附件发送。为此,我尝试了以下方法。

sendmail_touser() {
cat - ${Input_file_HTML} << EOF | /usr/sbin/sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: 
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 附件的电子邮件,而不是将其写在电子邮件正文中。你能请 help/guide 我也一样吗?我使用 outlook 作为电子邮件客户端。我什至删除了上面命令中的 cat 命令,但它也不起作用。

改用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=""
    TO=""
    SUBJECT=""
    FILEPATH=""
    CONTENTTYPE=""

    (
    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"