Problems sending email with HTML body and PDF attachment using mailx

Hi, I am using the below example command to send an email with a pdf attachment and text email body. I have tried using the below with the body being a *.txt file and it works fine. When I try to use a *.html file it only shows the html tags in the email body but not the actual html format. Is there anyway I can do that?

(cat body :wink: (uuencode attch1.pdf attch1.pdf :wink: | mailx -s "testing" "abc@gmail.com"

Hi @scupper01,

sending an email with mailx including inline html and an attachment is afaik not possible, because you need different Content-Type headers incl. a boundary. You could try the following bash script, which uses sendmail and a here document:

#!/bin/bash

FROM="foo@bar.org"
TO="bar@foo.edu"
SUBJECT="fancy subject"

PDFFILE="/path/to/file.pdf"
HTMLFILE="/path/to/file.html"

BOUNDARY=$(date +%s)

cat << EOMAIL | sendmail $TO
From: $FROM
To: $TO
Subject: $SUBJECT
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=$BOUNDARY

--$BOUNDARY
Content-Type: text/html

$(< $HTMLFILE)

--$BOUNDARY
Content-Type: application/pdf
Content-Disposition: attachment; filename="$PDFFILE"
Content-Transfer-Encoding: base64

$(base64 $PDFFILE)

--$BOUNDARY--
EOMAIL

Thanks for your help!

HI @scupper01,

you're welcome, I hope it works. But there's a small mistake in it: instead of filename="$PDFFILE", it has to be filename="${PDFFILE##*/}" or filename="$(basename $PDFFILE)", i.e. the path has to be stripped.