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

Rav*_*h13 3 linux shell scripting sendmail

我想在bash. 电子邮件应该通过阅读来获取它的正文,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
}
Run Code Online (Sandbox Code Playgroud)

上面的命令给出了一封仅包含 附件的电子邮件,Input_file_HTML并没有将其写在电子邮件正文中。你能帮我/指导我吗?我使用 Outlook 作为电子邮件客户端。我什至删除了cat上面命令中的命令,但它也不起作用。

Nic*_*ull 6

使用mutt呢?

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com
Run Code Online (Sandbox Code Playgroud)

mutt在 Debian 系统上安装:

sudo apt-get install -y mutt
Run Code Online (Sandbox Code Playgroud)

编辑如果您只能使用,请尝试此操作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
}
Run Code Online (Sandbox Code Playgroud)

像这样使用:

sendmail_attachment "to@example.com" "from@example.com" "Email subject" "/home/user/file.txt" "text/plain"
Run Code Online (Sandbox Code Playgroud)