Applescript to create outgoing message in Mail.app with specified sender

applescriptmail.app

I have a lot of accounts in Mail. I use KeyboardMaestro to automate much of what I do, but I haven't found a way to reliably get it to choose a given account in a new message window. I'm wondering if I can get applescript to do it for me.

I have the following:

tell application "Mail" to make new outgoing message with properties {sender:"my@email.address"}

and Mail obediently opens a new message window…but doesn't bother to set the sender correctly 🙁

I've also tried using the entire string which appears in the Sender popup menu in the window (which seems to be the Alias, in the Accounts pane of Mail's prefs window), plus just the substring before the hyphen (which seems to be the Full Name in Mail's prefs). Nothing works.

Has anyone managed to get this to work?

Best Answer

From here

Slightly different format for the sender property.

tell application "Mail"
  set theOutMessage to make new outgoing message with properties {visible:true}
  tell theOutMessage
      make new to recipient at end of to recipients with properties {address:"first@mail.com"}
      set sender to "FirstN LastN <josh.stackexchange@gmail.com>"
      set subject to "Message Subject"
      set content to "Message Text"
  end tell
end tell

So yours would be...

tell application "Mail" to make new outgoing message with properties {sender:"FirstN LastN <my@email.address>"}

You can find and edit the FirstN and LastN in System Preferences>Internet Accounts>[Email Name]>Details>Name