Trombone0904
Trombone0904

Reputation: 4258

OS X swift send mail with attachment

i working with swift for osx. i have this code for sending mails:

let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
service.recipients = ["[email protected]"]
service.subject = "My Subject"
service.perform(withItems: ["My Message"])

but i would like attach a pdf file. how can i realize it ?

Upvotes: 1

Views: 1049

Answers (1)

Jeisson Huerfano
Jeisson Huerfano

Reputation: 147

this work see

let email = "your email here"
let path = "/Users/myname/Desktop/report.txt"
let fileURL = URL(fileURLWithPath: path)

let sharingService = NSSharingService(named: NSSharingServiceNameComposeEmail)
sharingService?.recipients = [email] //could be more than one
sharingService?.subject = "subject"
let items: [Any] = ["see attachment", fileURL] //the interesting part, here you add body text as well as URL for the document you'd like to share

sharingService?.perform(withItems: items)

Upvotes: 3

Related Questions