Macos – How to send a HTML email from Cocoa


I'm looking for a way to create a HTML formatted email from a OS X Cocoa application.

My preferred workflow would be: The user selects a menu item and the default mail application opens with a pre-filled new email in the foreground.

I'm able to do this with mailto and -[NSWorkspace openURL] for plain text emails, but this doesn't work for HTML emails.

Best Solution

I was interested in this too, so two days of reverse engineering Safaris 'Mail Contents of This Page' feature and I got it working.

UPDATE: I improved the code and put it on GitHub

- (void)mailWebArchive:(WebArchive *)webArchive title:(NSString *)aTitle URL:(NSString *)aURL {
NSString *bundleID = @"";
NSData* targetBundleID = [bundleID dataUsingEncoding:NSUTF8StringEncoding];
NSAppleEventDescriptor *targetDescriptor = nil;
NSAppleEventDescriptor *appleEvent = nil;

targetDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID
appleEvent = [NSAppleEventDescriptor appleEventWithEventClass:'mail'
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithDescriptorType:'tdta'
                                                                               data:[webArchive data]]
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:aTitle]
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:aURL]
                    forKeyword:'url '];

NSAppleEventDescriptor *replyDescriptor = nil;
NSAppleEventDescriptor *errorDescriptor = nil;
AEDesc reply = { typeNull, NULL };  

// Send the AppleEvent
OSStatus status = AESendMessage([appleEvent aeDesc],
if(status == noErr)
    replyDescriptor = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
    errorDescriptor = [replyDescriptor paramDescriptorForKeyword:keyErrorNumber];
    if(errorDescriptor != nil)
        status = [errorDescriptor int32Value];

    if(status != noErr)
        NSLog(@"%s error %d", _cmd, status);

This code doesn't check if Mail is running, so it's only working when Mail is already started.

The pro side of this approach that it works with all email clients which implement MailLinkSupported and MailPageSupported. See QA1722.

The downside is that you can't set recipients like with a mailto. For this the Scripting Bridge seems the only solution. See this modified SBSendEmail sample.