1

Im wondering if there is a way to compose an email in Swift and have it populate fields such as CC and BCC.

Currently I am using NSSharingService and it only appears to have the options for Recipient, Subject, and Body.

I'm not married to using NSSharingService, but I would like to know a way to implement what I am trying to do.

EDIT: I am developing for Mac and not iOS

class SendEmail: NSObject {
    static func send(recipient: String) {
        let service = NSSharingService(named: 
        service.recipients = ["Recipient Goes Here"]
        service.subject = "Subject Goes Here"
        service.perform(withItems: ["Body Goes Here"])
    }
}
paull
  • 143
  • 6
  • It would appear that NSSharingService doesn't offer such a thing. – Lucas Derraugh May 17 '19 at 06:01
  • https://stackoverflow.com/questions/39660096/nssharingservice-set-cc-and-bcc-recipients-in-default-email-mac-osx-application/39673723 – pompopo May 17 '19 at 08:26
  • I have tried the solution in the link posted by @pompopo but I get all kinds of errors. I assume the post being three years old some of the classes used are deprecated. I get "Use of unresolved identifier 'openURL'" "Use of unresolved identifier 'sharedWorkspace'" or "Cannot call value of non-function type 'NSWorkspace'" along with a few syntax error. I'm assuming that I am the one doing something wrong here. – paull May 17 '19 at 12:11

1 Answers1

3

This works in Swift5 (Xcode 10.2.1, macOS 10.14.5)

let url = URL(string: "mailto:to@example.com?subject=subject&cc=cc@example.com&bcc=bcc@example.com")!
NSWorkspace.shared.open(url)
pompopo
  • 929
  • 5
  • 10