Sending an email from your app with an image attached in Swift
You need to add an attachmentData
to your mail, encoding your image
in an NSData. This is an example that show you how to send an email with
your image. I'm suppose that you have a UIViewController
where you can put the function sendMail
.
import MessageUI
class MyViewController: UIViewController, MFMailComposeViewControllerDelegate
{
// .... your stuff
func sendMail(imageView: UIImageView) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self;
mail.setCcRecipients(["yyyy@xxx.com"])
mail.setSubject("Your messagge")
mail.setMessageBody("Message body", isHTML: false)
let imageData: NSData = UIImagePNGRepresentation(imageView.image)!
mail.addAttachmentData(imageData, mimeType: "image/png", fileName: "imageName.png")
self.presentViewController(mail, animated: true, completion: nil)
}
}
}
In order to dismiss the VC, include the following method in your ViewController
:
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
The documentation for this method is reported in MFMailComposeViewControllerDelegate.
Send mail with file attachment
It seems that attachment
in mailto:
URLs are not supported on macOS (not always at least...details seems sketchy dependent on where you look on the internet :))
What you can use instead I found out from this blog post, is an instance of NSSharingService
documented here
Here is an example demonstrating how to use it.
And in your case you could do something like:
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)
Update
So @Spire mentioned in a comment below that this won't attach a file.
It seems there is a gotcha to be aware of.
For this to work you need to look into your App Capabilities.
You can either:
- disable App Sandbox
- enable read access for the folders from where you would like to fetch content.
I've attached a couple of screenshots.
Here is how this looks if I have disabled App Sandbox under Capabilities
And here is an image where I have enabled App Sandbox and allowed my app to read content in my Downloads
folder
If I do the above, I can access my file called document.txt
, located in my Downloads folder, using this URL
let path = "/Users/thatsme/Downloads/document.txt"
let fileURL = URL(fileURLWithPath: path)
And attach that to a mail
Hope that helps you.
Related Topics
Swift: Nsstatusitem Menu Behaviour in 10.10 (E.G. Show Only on Right Mouse Click)
Swift Memory Management: Storing Func in Var
Why It Is Called the Memberwise Initialiser
How to Grant Discoveruserinfowithuserrecordid Permission
How to Make Swiftui Uiviewrepresentable View Hug Its Content
Closures Return Value (Previously Completionblock)
Xcode UI Test:Accessibility Query Fail on Uitableviewcell
Using a Metal Shader in Scenekit
How to Prevent Eventstore Access Error on First Run
Swift: Lazily Encapsulating Chains of Map, Filter, Flatmap
Alamofire 3 Custom Encoding to Alamofire 4 Custom Encoding
Get Color of Point in a Skscene Swift
Shared Cookies with Wkprocesspool for Wkwebview in Swift
Firebase References Undeclared
App Crashes from IPA File But Runs Fine from Xcode