Getting mail message contents as html

Hello,
I can see from the Dictionary in Script Debugger that I can get the content property from a mail.app mail message object and it returns rich text. But I think that the sender of the messages sends them in HTML. So I suspect that Apple Mail converts the HTML to rich text.
How can I go about converting it back to HTML ? I know I can parse the rich text and that’d be it, but is there some way that is more expedite, like a function call or something ?
Thanks,
Regards

You have two options.

One is to take the rich text and convert it to HTML (there are a number of text conversion tools to do this).

The other is to ask Mail.app for the source of the message, not the content

HTML messages are sent as multipart messages with the content in multiple forms (plain text, html, etc.) so that the mail client can choose which version to display.

When you ask for the content of a message, you get one version of that, and that’s why you’re only seeing the rich text version.

Instead you can parse the message to find the HTML version of the message.

Somewhere in the message headers you’ll have a header that looks like:

Content-Type: multipart/alternative; boundary=“00000000000005945e065e0861ad”

So you can parse the message based on that ID to find the HTML.

Here’s a quick and dirty example I threw together based on pure AppleScript (no objectiveC, but there’s undoubtedly standard methods to handle the text parsing there if you prefer):

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Mail"
	-- get the current selected message
	set theMessage to item 1 of (get selection)
	-- get its header block
	set theHeaders to (all headers of theMessage)
	-- and turn it into a list of headers
	set theHeaders to paragraphs of theHeaders
	-- reverse it (the header we want is typically at the end, so we can optimize speed by reversing the headers
	set theHeaders to reverse of theHeaders
	
	--	set theHeaders to reverse of (paragraphs of (all headers of theMessage))
	set theSource to source of theMessage
end tell

-- now iterate through the headers
repeat with each_item in theHeaders
	-- find the 'Content-type' header
	if each_item contains "Content-Type: multipart" then
		-- extract the unique Boundary ID
		set boundaryID to last word of each_item
		exit repeat
		-- if we get here there's no multipart header
		return "Not a multipart message"
	end if
end repeat

-- standard AppleScript text manipulation to break the source text into
-- discrete chunks based on the multipart Boundary ID
set {oldTID, my text item delimiters} to {my text item delimiters, boundaryID}
set multiparts to text items of theSource
set my text item delimiters to oldTID

-- now walk through the parts
repeat with each_part in multiparts
	-- find the HTML version of the message
	if each_part contains "text/html" then
		set HTML_part to text of each_part
		exit repeat
		-- if we get here there's no HTML version
		return "No HTML found"
	end if
end repeat


-- now we have the HTML chunk
-- Strip the boundary headers
-- find the first HTML tag
set firstTag to offset of "<" in HTML_part
-- and find the last one
set lastTag to offset of ">" in ((reverse of characters of HTML_part) as text)
--finally return the HTML chunk
set theHTML to characters firstTag through -lastTag of HTML_part as text

Note that this is bare bones (limited error checking), and that ‘HTML email’ is a nebulous thing - in my testing some HTML messages include the <HTML> tags, others don’t. There may also be encoding issues to deal with (quoted printable, base64, etc.) which can complicate things, but hopefully this will get you going.

I will do this on my code and post here there results.

Thanks,
Regards

Hello,
My code is working with this example of getting the HTML out of the message.

Many thanks for your help,
Regards