0
votes

I'm still stuck on how to generate email drafts from a web application that can be downloaded from a browser and then opened and sent by MS Outlook for Mac.

I tried:

Do you have any ideas?

1

1 Answers

0
votes

Workaround (only applicable if you can ask your users to install an application on their machines). I assume this approach can also be applied to other languages.

Create a small Java application which registers an open URI handler:

Desktop.getDesktop().setOpenURIHandler(e -> handleOpenUri(e));

Run jpackage on a Mac, then copy the generated Info.plist file from the app bundle.

Add these lines to Info.plist to register a custom URL scheme, e.g. 'customscheme':

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>customscheme</string>
    </array>
    <key>CFBundleURLName</key>
    <string></string>
  </dict>
</array>

Use the jpackage --resource-dir option to provide the modified Info.plist.

On the web app side, instead of generating a draft file to download, render a URL with the custom scheme, e.g.:

customscheme://https://www.server.com/EmailDraft?id=...&token=...

In the open URI handler in the Java app, download the draft info from the web server, e.g.:

  private void handleOpenUri(OpenURIEvent e) {
    String uri = e.getURI().toString();
    URL url = new URL(uri.substring("customscheme://".length()));
    JSONObject json = new JSONObject(IOT.streamToString(url.openStream()));
    String recipient = json.getString("recipient");
    String subject = json.getString("subject");
    String message = ST.replaceLineBreaks(json.getString("message"), "<br>");
    String attachmentName = json.getString("attachmentName");
    byte[] attachmentBytes = Base64.getDecoder().decode(json.getString("attachmentBytes"));
  }

Save the downloaded attachment to a temporary file.

Generate an Apple Script and save it to a temporary file:

tell application "Microsoft Outlook"
    activate
    set theRecipient to "<recipient>"
    set theSubject to "<subject>"
    set theContent to "<message>"
    set theFile to "<attachment-tmp-file>" as POSIX file
    set theMessage to make new outgoing message with properties {subject:theSubject, plain text content:theContent}
    make new recipient with properties {email address:{address:theRecipient}} at end of to recipients of theMessage
    make new attachment at the end of theMessage with properties {file:theFile}
    open theMessage
end tell

And finally execute the script (using ProcessBuilder):

osascript <generated-apple-script-file>

When you click the link with the custom scheme in a browser, it will ask if the site is allowed to open your Java application. If confirmed, the application will launch, receive the open URI event, prepare and execute the Apple Script, which in turn will open the Outlook message draft, complete with attachment, ready to send.