Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Mail.app From An External Application With An Attachment and Subject

I'd like to open Mail.app and specify a Subject and a File to attach. I can do either independently but not both.

To set the subject I can just form a mailto: string and NSWorkspace openURL that.

To set an attachment I can use

[[NSWorkspace sharedWorkspace] openFile:resolvedPath withApplication:@"Mail"];

I'm not aware of a equivalent to iOS's MFMailComposeViewController for the Mac. What are my options?

like image 239
NeilInglis Avatar asked Dec 28 '22 09:12

NeilInglis


1 Answers

NSString* subject = @"mail subject";
NSString* body = @"mail body";
NSString* to = @"[email protected]";

NSString *encodedSubject = [NSString stringWithFormat:@"SUBJECT=%@", [subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *encodedBody = [NSString stringWithFormat:@"BODY=%@", [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *encodedTo = [to stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedURLString = [NSString stringWithFormat:@"mailto:%@?%@&%@", encodedTo, encodedSubject, encodedBody];
NSURL *mailtoURL = [NSURL URLWithString:encodedURLString];

[[NSWorkspace sharedWorkspace] openURL:mailtoURL];
like image 140
denis2342 Avatar answered Feb 07 '23 08:02

denis2342