Skip to content
Advertisement

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?

Advertisement

Answer

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
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement