E-mail form to send plain text, HTML, and multiple attachments

I am trying to create an e-mail form that will send plain text, html, and up to two attachments. Without the HTML portion of things, I have everything working just fine, however, as mail clients need multipart/alternative specified in order to know whether to display either plain text or HTML, and then multipart/related specified in order to show multiple boundaries as one message, I am finding it impossible to get this working. I spent all day on it and have made headway but as there are no docs on this seemingly simple scenario, I have been unable to find a resolution.

This is my current statement. The issue is that the first dtml-boundary tag cannot be left blank or it defaults to making everything base64 encoded and the e-mail comes through as an attachment (the entire message). If I specify type="text/plain", etc. it inserts additional lines in the raw body of the email and thus it doesn't display in a mail client correctly (see below for examples).

Here is what I currently have.

<dtml-sendmail mailhost="MailHost">
To: <dtml-var primary_email>
<dtml-if "secondary_email!=None">
Cc: <dtml-var secondary_email>
</dtml-if>
From: <dtml-var email_from>
Subject: <dtml-var email_subject>
<dtml-mime type="text/plain" charset="us-ascii" encode="7bit" multipart="alternative"><dtml-var email_body_plain>
<dtml-boundary type="text/plain" charset="UTF-8" encode="7bit">
<dtml-mime type="text/html" charset="UTF-8" encode="7bit" multipart="related"><html><body><dtml-var email_body><br><br></body></html>
<dtml-boundary type_expr="REQUEST.get('file_type', attachment1.headers['Content-Type'] )" disposition="attachment" encode="base64" name_expr=attachment1.filename filename_expr=attachment1.filename><dtml-var expr="attachment1.read()"></dtml-mime>
</dtml-mime>
</dtml-sendmail>

This is what comes through in the raw email (pertinent section only):
--xx.xxx.xxx.xx.1230.13812.1592269201.208.191`
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: 7bit

Mime-Version: 1.0
Content-Type: multipart/related;
boundary="xx.xxx.xxx.xx.1230.13812.1592269201.209.192"

--xx.xxx.xxx.xx.1230.13812.1592269201.209.192

This does not display correctly, however the following does (note that there must be the boundary and then no space before the Mime-Version but for the life of me I can't figure out how to accomplish this. I got it to work by editing the raw email by hand and testing it out in Thunderbird):

--xx.xxx.xxx.xx.1230.13812.1592269201.208.191
Mime-Version: 1.0
Content-Type: multipart/related;
boundary="69.164.211.16.1230.13812.1592269201.209.192"

--69.164.211.16.1230.13812.1592269201.209.192

Without the attachment, plain text and html work perfectly. Without the html portion, plain text and both a single attachment and/or two attachments work perfectly.

Use Python's email module instead of more than two decades old and practically obsolete dtml-sendmail stuff..this is really not the way for composing proper emails nowadays.

Appreciated, but this is the way I am doing this and really just need to figure out the solution. I'm not interested in starting over. Like I said, everything is working except this one situation.

One of the reasons for changing horses...but your choice. Every code has its time and it is sometimes better to rewrite such a simple piece of code in a proper way rather than finding and fixing issues with in a broken functionality and broken implementation like dtml-sendmail...and now I am silent ...

I solved it. Took me almost 12 hours of searching through code and thinking about hierarchical structure but I got it working. If anyone can find a flaw in how I did it, please let me know.

<dtml-sendmail mailhost="MailHost">
To: <dtml-var primary_email>
<dtml-if "secondary_email!=None">
Cc: <dtml-var secondary_email>
</dtml-if>
From: <dtml-var email_from>
Subject: <dtml-var email_subject>
<dtml-mime type="multipart/alternative" multipart="mixed" encode="7bit">
<dtml-mime type="text/plain" multipart="alternative" charset="us-ascii" encode="7bit"><dtml-var email_body_plain>
<dtml-boundary type="text/html" charset="UTF-8" encode="7bit"><html><body><dtml-var email_body><br><br></body></html></dtml-mime>
<dtml-boundary type_expr="REQUEST.get('file_type', attachment1.headers['Content-Type'] )" disposition="attachment" encode="base64" name_expr=attachment1.filename filename_expr=attachment1.filename><dtml-var expr="attachment1.read()"></dtml-mime>
</dtml-sendmail>

Plone Foundation Code of Conduct