When creating email messages you are supposed to set the Content-Type to multipart/alternative
when sending HTML and TEXT or multipart/mixed
when sending TEXT and attachments.
So what do you do if you want to send HTML, Text, and attachments? Use both?
I hit this issue. This architecture (from Lain's answer) worked for me. Here is the solution in Python.
Here is the main email creation function:
Here is the full code for sending an email containing html/text/attachment:
I hit this challenge today and I found these answers useful but not quite explicit enough for me.
Edit: Just found the Apache Commons Email that wraps this up nicely, meaning you don't need to know below.
If your requirement is an email with:
The only structure I found that works with Gmail/Outlook/iPad is:
And the code is:
And an example of using it with from Gmail
Here is the best: Multipart/mixed mime message with attachments and inline images
And image: https://www.qcode.co.uk/images/mime-nesting-structure.png
Schema multipart/related/alternative
Use
multipart/mixed
with the first part asmultipart/alternative
and subsequent parts for the attachments. In turn, usetext/plain
andtext/html
parts within themultipart/alternative
part.A capable email client should then recognise the
multipart/alternative
part and display the text part or html part as necessary. It should also show all of the subsequent parts as attachment parts.The important thing to note here is that, in multipart MIME messages, it is perfectly valid to have parts within parts. In theory, that nesting can extend to any depth. Any reasonably capable email client should then be able to recursively process all of the message parts.
Messages have content. Content can be text, html, a DataHandler or a Multipart, and there can only be one content. Multiparts only have BodyParts but can have more than one. BodyParts, like Messages, can have content which has already been described.
A message with HTML, text and an a attachment can be viewed hierarchically like this:
And the code to build such a message:
Great Answer Lain!
There were a couple things I did to make this work in a broader set of devices. At the end I will list the clients I tested on.
I added a new build constructor that did not contain the parameter attachments and did not use MimeMultipart("mixed"). There is no need for mixed if you are sending only inline images.
In addTextVersion method I added charset when adding content this probably could/should be passed in, but I just added it statically.
The last item was adding to the addImagesInline method. I added setting the image filename to the header by the following code. If you don't do this then at least on Android default mail client it will have inline images that have a name of Unknown and will not automatically download them and present in email.
So finally, this is the list of clients I tested on. Outlook 2010, Outlook Web App, Internet Explorer 11, Firefox, Chrome, Outlook using Apple’s native app, Email going through Gmail - Browser mail client, Internet Explorer 11, Firefox, Chrome, Android default mail client, osx IPhone default mail client, Gmail mail client on Android, Gmail mail client on IPhone, Email going through Yahoo - Browser mail client, Internet Explorer 11, Firefox, Chrome, Android default mail client, osx IPhone default mail client.
Hope that helps anyone else.