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

标签 linux shell scripting sendmail

<分区>

我想在 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 命令,但它也不起作用。

最佳答案

改用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"

关于linux - Sendmail 命令将文件作为电子邮件正文和附件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38790167/

相关文章:

scripting - 使用 Clojure 创建符号链接(symbolic link)

linux - 如何在 csh 中获取第二个参数直到最后一个参数?

android - ADB Start Activity 名称不起作用

ios - Xcode-命令行构建

python - 在 Python 中,如何有效地管理脚本文件之间的引用?

windows - 如何获取映射网络驱动器的驱动器号

linux - Vim 语法颜色对某些文件关闭

python - 如何使用 python-xlib 管理我的应用程序窗口?

json - 使用 yum 创建 repo 以安装文件列表

linux - 安装 Bash 的第二个实例(使用不同的配置)