Mail.app: Trick to Make new Message with HTML text

Many users already know that newer versions of Mail.app don’t have the ability to set the HTML content of a new message.

I’ve been digging around a little in the secrets of the application and came up with a way to get around this limitation. In the following script, you only need to edit the HTML body.

Read more here about Request For Comments (RFC) 822 standard


-- script: Make new Message with HTML body
-- written: by KniazidisR (today)

set theHTMLBody to "<h1 style=\"color:green;\">Test Note 1</h1> 
<p style=\"color:red;\" >Blah, blah, blah</p>" -- EDIT THIS FOR YOUR NEEDS

-- make temporary EML file
set emlFile to my makeTempFile()
-- compose the EML content according to RFC-2822 (specification)
set emlText to my makeEMLwithHTMLbody:theHTMLBody
-- write EML content to temporary file
my write_to_file(emlFile, emlText)
-- make new message with contents of temporary file
my newMessageWithContentsOfFile:emlFile


------------------------------------------------- HANLERS --------------------------------------------------

on newMessageWithContentsOfFile:emlFile
	tell application "Mail"
		open file emlFile
		activate
		delay 1
	end tell
	tell application "System Events" to keystroke "e" using {shift down, command down}
end newMessageWithContentsOfFile:

on makeTempFile() -- makes temporary EML file at temporary items folder of user domain
	set tempFolder to (path to temporary items from user domain) as text
	tell application "Finder"
		try
			set emlFile to make new file at folder tempFolder with properties {name:"contentBuffer.eml"}
		on error number -48 -- file already exists 
			set emlFile to file "contentBuffer.eml" of folder tempFolder
		end try
		set emlFile to emlFile as text
	end tell
	return emlFile
end makeTempFile

on makeEMLwithHTMLbody:HTMLbody -- composes the EML content according to RFC-2822 specification
	set theHeaders to "
Subject: Saying Hello
Date: Fri, 21 Nov 1997 09:55:06 -0600
Message-ID: <1234@local.machine.example>
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=us-ascii
Mime-Version: 1.0

"
	set htmlTop to "<!DOCTYPE html>
<html lang=3D\"en\" xmlns=3D\"http://www.w3.org/1999/xhtml\" xmlns:o=3D\"urn:sch=
emas-microsoft-com:office:office\" xmlns:v=3D\"urn:schemas-microsoft-com:vml\"=
><body width=3D\"100%\" style=3D\"margin: 0; padding: 0 !important; background:=
#f3f3f5; mso-line-height-rule: exactly;\">
"
	
	set htmlBottom to "</body>
</html>"
	
	set emlText to theHeaders & htmlTop & HTMLbody & htmlBottom
	return emlText
end makeEMLwithHTMLbody:

on write_to_file(the_file, the_text) -- writing to file
	set file_ID to open for access the_file with write permission -- Clear any existing content if the file already exists:
	set eof file_ID to 0
	-- Add the UTF-8 encoded text:
	write the_text to file_ID
	close access file_ID
end write_to_file

Hi KniazidisR

Unsure if this is appropriate to post here, but would ask if it is possible to do the reverse, that is to convert eml to HTML or directly save mail message to html.

I did search but could not find an example of applescript.

Thank you

Super clever trick.

Under Ventura I get a “file permission error” for

set file_ID to open for access the_file with write permission

How can I make the new email editable?

I am on Catalina now and can’t install newer OS versions. So, I can’t help you with Ventura.

EML files can’t be saved as HTML, because the HTML is only part of EML files. Other parts is the headers, which can’t be saved as HTML. They are plain text content. You can archive the messages only as .eml files. To see the content of some .eml file, open it with TextEdit.

Eml files can be converted to html because what you see in an email client is html anyways. It just can’t be done in AppleScript. My application Mail Archiver X uses html as internal representation for the body of an email.

@One208: The question is: what do you want to do with the html?

The EML format is HTML + special headers above and below. And I’m perfectly capable of archiving messages into an .eml file using plain AppleScript. Without any additional applications and cutting off the head from the body. Personally, it is not clear to me how a headless saved message can be useful. And why should I necessarily violate this natural conventional format.

Saving chosen message as .eml file using plain AppleScript:


-- get name and source of chosen message
tell application "Mail"
	set aMessage to item 1 of (get selection)
	set richSource to (source of aMessage) as rich text
	set theSubject to subject of aMessage
end tell

-- replace every ":" symbol in theSubject with "_"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set {itemList, AppleScript's text item delimiters} to {text items of theSubject, "_"}
set {theSubject, AppleScript's text item delimiters} to {itemList as text, ATID}

-- build destination file's HFS path
set outputFile to (((path to desktop folder) as text) & theSubject & ".eml")

-- write rich text to EML file
try
	set fileReference to open for access file outputFile with write permission
	set eof of fileReference to 0
	write richSource to fileReference
	close access fileReference
on error
	try
		close access file outputFile
	end try
end try

Hi KniazidisR

Thank you for your suggestion to save email as .eml file, which I have in past. Opening it with Textedit does not render the pictures.

Most emails have pictures !

I saw your post (link below) with html →pdf handler. I wanted to use the handler to save email as pdf

email: eml→ html → pdf

Its a roundabout way, but gui scripting to save email as pdf was flaky

https://macscripter.net/viewtopic.php?pid=209959

Cheers

I do not want to continue this topic further, because the conviction of the unbelievers tires.

You must understand that stuffing Mail.App to the HTML file is also impossible to shove a barrel into the herring. Because in fact, we are stuffing the opposite, a herring in a barrel.

And the link indicated by you is to work with Notes.app, which are HTML files, without any headers. Notes and messages are different things.

As for pictures that are not built into the HTML part of the message, you should give permission to download them from the network (and time) before exporting the message to the .eml file. You must understand that they are not in your message initially until they download.

To update remote content, add following at beginning of eml saving script:


tell application "Mail"
	activate
	set download html attachments to true -- load remote content as well
end tell

Hey KniazidisR, totally believe you and you have helped many a times in past.
I have saved emails as .eml, but sometimes for some email I just need a pdf
I have used EML to PDF Converter (Link below) which uses HTML as intermediate step before saving it as pdf (which needs wkhtmltopdf)
I was excited when I saw you post the html → pdf handler, hence the request.
EML to PDF Converter
https://www.whitebyte.info/publications/eml-to-pdf-converter
Thank you for your help