【问题标题】:Sendmail command to send a file as an email body as well as attachment [duplicate]Sendmail命令将文件作为电子邮件正文和附件发送[重复]
【发布时间】:2016-08-05 13:11:49
【问题描述】:

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

【问题讨论】:

    标签: linux shell scripting sendmail


    【解决方案1】:

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

    【讨论】:

    • 您好,尼克,感谢您的回复。我不是 root,也没有 mutt 命令。能否请您在这里指导我,将不胜感激。
    • 可以请系统管理员安装吗?这是实现您想要的非常简单的方法。
    • @RavinderSingh 该更新可能对您有用。
    • 你好尼克:它给了我以下错误。 sendmail: invalid option -- 's' 不,我相信他们不会安装 mutt。
    • @RavinderSingh 查看更新。
    猜你喜欢
    • 2014-02-09
    • 2013-03-19
    • 2018-07-23
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2013-07-11
    • 2015-12-21
    • 2021-01-01
    相关资源
    最近更新 更多