Formated eMail body?

Is there a way to create an eMail with a formated body? As far as i can see under http://wiki.keyboardmaestro.com/action/Send_Mail_Message this doesn’t seem to be the case. Or is there a workaround possible?

I’m sure you could do this by controlling one of your mail apps using KM and/or AppleScript. And maybe even GMail. Is there some reason not to use one of those?

I don’t know how to use AppleScript. Do you have a simple sample script I could take a look at?

This is a bit old, but it should get you started:
Introduction to Scripting Mail

And we actually have a thread on Getting Started with AppleScript here:

Here’s another example, much more recent:
Sending emails with the Apple Mail program using AppleScript

You might do a google on “AppleScript mail” to see a lot of other results.

Hey flaimo,

It can be done, but it’s a very ugly workaround.

You cannot create and work with a message and then send it — you must create and send immediately without making the message visible.

But it works.

-Chris

------------------------------------------------------------
# Auth: Mark Munro (Macscripter)
# Mods: by Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2013/08/04 01:09
# dMod: 2015/08/11 10:03
# Appl: Mail
# Task: Make new HTML email!
# Tags: @Applescript, @Script, @Mail, @New, @Message, @HTML, @Content
------------------------------------------------------------

set toName to "Addressee Name"
set toAddress to "addressee@emailprovider.com"
set textSubject to "HTML Test"

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

mailMessageCreate(toName, toAddress, textSubject, textBody)

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------

on mailMessageCreate(toName, toAddress, textSubject, textBody)
  tell application "Mail"
    activate
    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}
      send
    end tell
  end tell
end mailMessageCreate

------------------------------------------------------------
1 Like

Thanks for sharing your script. Unfortunately seeing/editing the message before sending is important in my case. My plan would be to semi-automate the creation of meeting invitations and I need to be able to copy talking points into the mail body.

If the API for creating new emails also accepts HTML, then maybe support for this could be added to the Send Mail action in a future version of Keyboard Maestro.

Keyboard Maestro uses AppleScript to talk to Mail, so that’s not especially likely.

I have been frustrated with Mail’s shortcomings, since I gave up on Eudora and started using it in 2005.

Apple has done little to improve its AppleScript during those 10+ years…

In Yosemite you can write custom html to an email FILE in the drafts folder and then get Mail to open it, but the trick is getting Mail to recognize the file.

(This didn’t work pre-Yosemite.)

So it looks like you could keep messages in a templates folder in Mail and then via script:

Copy the relevant one to the Drafts mailbox.

Find it on-disk.

Write new html content to it.

<<END SCRIPT

So far I don’t see a way to open it for editing with a script, but this can be done with Keyboard Maestro and/or System Events.

Hmm… Okay, testing reveals that my script can be made to work on Yosemite.

You still have to handle the newly made message for editing, but that’s not too hard.

------------------------------------------------------------
# Auth: Mark Munro (Macscripter)
# Mods: by Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2013/08/04 01:09
# dMod: 2015/08/12 18:25
# Appl: Mail
# Task: Make new HTML email!
# Tags: @Applescript, @Script, @Mail, @New, @Message, @HTML, @Content
------------------------------------------------------------

set toName to "Addressee Name"
set toAddress to "addressee@emailprovider.com"
set textSubject to "HTML Test"

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

mailMessageCreate(toName, toAddress, textSubject, textBody)

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------

on mailMessageCreate(toName, toAddress, textSubject, textBody)
  tell application "Mail"
    activate
    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}
      save
    end tell
    close front window
  end tell
end mailMessageCreate

------------------------------------------------------------

-Chris