Sending an Email from Your App with an Image Attached in Swift

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

App Sandbox disabled

And here is an image where I have enabled App Sandbox and allowed my app to read content in my Downloads folder

App Sandbox enabled

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



Leave a reply



Submit