Applescript - sending html email in

Has anyone had any luck writing this applescript - mine leaves a “ghost” message in mail after you quit the application - 10.8.2

Please show your code so we can know what you’re working with.

When I convert some applescript to VBA I found a bug that lookst like yours

I think your problem is that you have set visible:false
If you change it to true do you have the same problem then ?

When I play with it it was a bug in Lion and fixed in Mountain Lion

One of my VBA pages where I use a function with this info

set textBody to "<HTML>
<BODY bgcolor=\"#99CCFF\">
   <H1>Hi There</H1>
   <p>This is very minimal <u>\"hello world\"</u> HTML document.</p> 
This works.

set textSubject to "HTML Test"
set toAddress to ""
set toName to "John Doe"

mailMessageCreate(toName, toAddress, textSubject, textBody)

on mailMessageCreate(toName, toAddress, textSubject, textBody)
	set pathToFile to "Macintosh HD:Users:mark_munro:Desktop:Justice.rtf"
	tell application "Mail"
		set refMessage to make new outgoing message with properties {subject:textSubject, visible:false} -- visible MUST be false to work
		tell refMessage
			set html content to textBody -- must set the HTML CONTENT rather than the CONTENT
			make new to recipient at end of to recipients with properties {name:toName, address:toAddress}
		end tell
	end tell
end mailMessageCreate

I would love to know how to include an attachment into my code above… if anyone has a clue.

Hi. The outgoing attachment needs to be an alias. I had this handy, which you can adapt.

set theSubject to (display dialog "Subject: " default answer "")'s text returned
set theContent to (display dialog "Message: " default answer "")'s text returned
set theRecipient to (display dialog "To: " default answer "")'s text returned

tell application "Mail"
	tell (make new outgoing message with properties {subject:theSubject, content:theContent, visible:true})
		(make new to recipient at to recipients's end with properties {address:theRecipient})
		make new attachment with properties {file name:choose file}
	end tell
end tell

I need to know how to include an attachment when using “html content” rather than “content”!

Perhaps if you add the attachment before creating the html message? Just a guess, did not try it yet…

Tried it. No result.

Guess #2 : Perhaps the file attachment needs to be included in the HTML code, somewhere, somehow? A file:// URL link?

Hi. Interestingly, HTML content isn’t documented in the Mail dictionary, so using that command is guesswork, and it seems to wipe out any standard attachment. If you want to add an inline file, it must be hosted and rolled into the HTML.

set textBody to "<HTML>
<BODY bgcolor=\"#99CCFF\">
<H1>Hi There</H1>
<p>This is very minimal <u>\"hello world\"</u> HTML document.</p> 

<a><img src=\"\" alt=\"Whatever\"></a>
  <a href=\"http://www.insert_real_address.rtf\">Some Text Document</a>

–edited for quote commenting

P.S. It may be easier to create the HTML and then email the contents directly from Safari, rather than fiddling with Mail.


To figure out what goes wrong, I’d have a look into the mail header, and see if the attachment is there, in both versions, with and without html contents. You can turn on logging, ( I don’t have that script at the moment).

But: Why not use textutil, and convert the html to rtf, and then paste it into the mail? (As an alternative to use Safari.) I haven’t tried it, and I’m unsure if you keep the background colour. I use to open the html in Safari and copy from there and paste into the mail application in order to get the background color. That been said; it may work to convert to rtf, copy to clipboard, ( cat rtf-file |pbcopy ) and then paste into the contents of the mail. (Maybe there is a way to set the background color afterwords via rtf and attributes of runs of text (by AppleScript, like in TextEdit ). If this is possible, then the regular mail attachment should work.

I am also totally sure that the regular html templates that comes with mail is working with attachments, so maybe a peek into those also may help.

I am not sure of the standard for tags in mail either, but the tags in the post above breaches for sure with the xhtml standard that specifies only lowercase tags. Not that that should matter, but I guess the mail templates, has some additional tags or specifiers that may be of interest when it comes html, I only hope it is easily discernable.

One thing I noticed when trying to add an attachment to the HTML message (and not sending it), is it ends up in the drafts folder. If you double-click it, it turns into a standard message, the html seems gone and the attachment is there.

But if you send it right away, both the received message and sent message are in html and no attachments are present.

Quite strange…