Swift : email for macOS

The email tools for iOS are quite extensive… however this is not so for macOS
there is no direct support for BCC, CC , or mixed content body (just plain text)

Does anyone have (or know) of a Swift Module that does support all this.

I can do some of it like BCC and CC using a “mailto” URL, but I’d like to do this a more “proper way”

SO seems to have several examples like

class SendEmail: NSObject {
    static func send() {
        let service = NSSharingService(named: NSSharingService.Name.composeEmail)!
        service.recipients = ["email@yourEmail.eu"]
        service.subject = "Email Subject"

        service.perform(withItems: ["Email Content"])
    }
}

nm - there doesnt appear to be any params/methods for bcc, cc etc exposed this way
and apple docs suck so bad now …
you get a service provider reference back but its pretty opaque

yeah… already have a version of that code… but it doesn’t support

  • BCC
  • CC
  • any kind of “body” other than plain text,

Yeah so I see and why I edited my previous comment

Not fun
And there doesnt seem to be a nice simple “email” class you can use which would

another disadvantage. that code you posted (which I just tested)… COMPOSES an email, but doesn’t actually send it, that required manual intervention… :frowning:

ok… I tried another method using MAILTO URL
generated a sample cmd

mailto:rdavids3@cox.net?subject=test%20Email&body=No%20message%20provided

attempted to execute it using

let url = URL(string:cmd)
NSWorkspace.shared.open(url!)

and it DID open my email app, and insert the correct data.

But is there a way to silently just SEND it?

NOTE : using the terminal command SENDMAIL is not an option as my ISP blocks the port

not with a mailto url that I know of
the URL handler just takes the URL and knows what to do with it - open mail & create a new mail with the right to, from, cc, bcc etc fields already filled in

The user has to actually click send

I did notice on SO there were some users also wanting to send emails automagically and there just didnt seem to be a decent way with whats exposed to Swift
One commented they used postfix, another mail service that is often running locally, to do that

seems crazy since I think you could do this with AppleScript
see AppleScript to send e-mail, using Apple's Mail, with multiple recipients, cc, bcc and attachments · GitHub
for source for such a thing

It might not be entirely silent as it will start mail if its not running
but you CAN do it so … it seems there should be a way in Swift

can swift send apple script commands to mail ?

Can’t use postfix as some ISP (including my own) block that method
Don’t want to use AppleScript as that is (my understanding) no longer AppStore acceptable

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

oops - saw on second reading that you already know this.

yeah… that works… .but requires the user to click “send”… I want it to be silent

I’m using MailJet APIs in both macOS and iOS to send email silently.
MailJet APIs support cc/bcc, text/html message, subjects, attachments …
My apps are using MailJet for:

  • sending UnhandledExceptions to support, silently,
  • sending automated mails (alerts, periodic reports, …), silently
  • contact/feedback forms, on request.

But of course you need a MailJet account and configuration (SPF, DKIM, DNS)
Free for 200 mails / day - 6000 / month.

Thanks, but for this project using a 3rd party program is not an option.
This is a framework to aid developers in creating programs (in a way similar to Xojo) and need to rely on just what the OS provides