简体   繁体   中英

Send email with html body and some attachment with sendmail

Follwing this https://stackoverflow.com/a/11725308/1507546 , I could send an email with one attachment.

However the body is always empty.

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
}

Why is my function sending emails with empty body and how to fix it?

I figured it out after playing with the headers, this worked for me.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM