使用 html body 发送电子邮件,使用 sendmail 发送一些附件

Send email with html body and some attachment with sendmail

按照这个 ,我可以发送一封带有一个附件的电子邮件。

然而 body 总是空的。

notify() {
    local mailpart="$(uuidgen)"
    local mailpartBody="$(uuidgen)"
    local subject="subject"
    local attachment='tmp/attachment.txt'


    (
    echo "From: no-reply@company.com"
    echo "To: ${authors}"
    echo "Subject: ${subject}"
    echo "MIME-Version: 1.0"
    echo "Content-Type: multipart/mixed; boundary=\"${mailpart}\""
    echo ""
    echo "--${mailpart}"
    echo "Content-Type: multipart/alternative; boundary=\"${mailpartBody}\""
    echo ""
    echo "--${mailpartBody}"
    echo "Content-Type: text/plain; charset=ISO-8859-1"
    echo "You need to enable HTML option for email"
    echo "--${mailpartBody}"
    echo "Content-Type: text/html; charset=ISO-8859-1"
    echo "Content-Disposition: inline"
    echo "<h1>hello world!!</h1>"
    echo "--${mailpartBody}--"

    echo "--${mailpart}"
    echo 'Content-Type: text/plain; name="'$(basename ${attachment})'"'
    echo "Content-Transfer-Encoding: uuencode"
    echo 'Content-Disposition: attachment; filename="'$(basename ${attachment})'"'
    echo ""
    uuencode ${attachment} $(basename ${attachment})
    echo "--${mailpart}--"
    ) | sendmail -t
}

为什么我的函数发送的电子邮件为空 body,如何解决?

我在玩了 headers 之后弄明白了,这对我有用。

notify() {
    local mailpart="$(uuidgen)"
    local mailpartBody="$(uuidgen)"
    local subject="subject"
    local attachment='tmp/attachment.txt'


    (
    echo "From: no-reply@company.com"
    echo "To: ${authors}"
    echo "Subject: ${subject}"
    echo "MIME-Version: 1.0"
    echo "Content-Type: multipart/mixed; boundary=\"${mailpart}\""
    echo ""
    echo "--${mailpart}"
    echo ""
    echo "Content-Type: text/plain; charset=ISO-8859-1"
    echo "Content-Disposition: inline"
    echo ""
    echo "<h1>hello world!!</h1>"

    echo "--${mailpart}"
    echo 'Content-Type: text/plain; name="'$(basename ${attachment})'"'
    echo "Content-Transfer-Encoding: uuencode"
    echo 'Content-Disposition: attachment; filename="'$(basename ${attachment})'"'
    echo ""
    uuencode ${attachment} $(basename ${attachment})
    echo "--${mailpart}--"
    ) | sendmail -t
}