Using CentOS 7 by default mailx (displayed as heirloom-mailx), I simplified this to a simple use of a text file with the required headers and a static border for a multi-part / mixed and multi-part / alternative installation.
I am sure you can figure out multipart / related if you want with the same setting.
test.txt:
--000000000000f3b2150570186a0e Content-Type: multipart/alternative; boundary="000000000000f3b2130570186a0c" --000000000000f3b2130570186a0c Content-Type: text/plain; charset="UTF-8" This is my plain text stuff here, in case the email client does not support HTML or is blocking it purposely My Link Here <http://www.example.com> --000000000000f3b2130570186a0c Content-Type: text/html; charset="UTF-8" <div dir="ltr"> <div>This is my HTML version of the email</div> <div><br></div> <div><a href="http://www.example.com">My Link Here</a><br></div> </div> --000000000000f3b2130570186a0c-- --000000000000f3b2150570186a0e Content-Type: text/csv; charset="US-ASCII"; name="test.csv" Content-Disposition: attachment; filename="test.csv" Content-Transfer-Encoding: base64 X-Attachment-Id: f_jj5qmzqz0
Borders define compound segments.
A border identifier that does not have a dash at the end is the starting point of the segment.
The one with two traits at the end is the end point.
In this example, there is a part in the main multipart / mixed section for multipart / alternative.
The multi-element / alternative method basically says: “Rollback to this IF the priority part is not executed” - in this example, HTML clients usually perceive priority as HTML. If the mail client does not display HTML, it returns to plain text.
The multipart / mixed method, which encapsulates the whole message, basically says that there is different content displaying both.
In this example, I posted the attachment as a CSV file to my email. You will see that the attachment is connected using base64 in the command below.
I added an attachment as an example, you will need to configure the type of content according to the attachment and indicate whether it will be embedded or not.
X-Attachment-Id is required for some providers, randomly identify the identifier you set.
The command to send by mail is:
echo -e "'cat test.txt; openssl base64 -e < test.csv'\n--000000000000f3b2150570186a0e--\n" | mailx -s "Test 2 $( echo -e "\nContent-Type: multipart/mixed; boundary=\"000000000000f3b2150570186a0e\"" )" -r fromaddress@example.com toaddress@example.com
As you can see in the mailx subject line, I statically insert a multilayer border, this is the first header that the mail client will see.
Then comes the dumping of the contents of test.txt.
As for attachments, I use openssl (which is pretty standard on systems) to convert a file attachment to base64.
In addition, I added a border closing operator at the end of this echo to mark the end of the message.
This works around heirloom issues and with little or no scripting.
Instead, the echo can be a feed or any other number of methods.