
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to send an attachment in email using Swift?
To send an email from our iPhone device using our application we need to import the MessageUI framework of iOS SDK. After importing the framework in your application, drag and drop a button on the view controller. Add empty action for that button.
Now add the following code in your view controller.
funccomposeEmail(to email: String,subject: String,Body: String) { if( MFMailComposeViewController.canSendMail()) { letmailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self mailComposer.setToRecipients([email]) mailComposer.setSubject(subject) mailComposer.setMessageBody(Body, isHTML: true) letpathPDF = "\(NSTemporaryDirectory())result.pdf" if let fileData = NSData(contentsOfFile: pathPDF) { mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "result.pdf") } self.present(mailComposer, animated: true, completion: nil) } else { print("email is not supported") } } funcmailComposeController(_ didFinishWithcontroller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { self.dismiss(animated: true, completion: nil) } }
Call this method inside the action of a button you just created.
@IBActionfuncactionButtonOne(_ sender: Any) { composeEmail(to: "ashish@xy.com", subject: "Saying Hi", Body: "Hey there, hope you are doing well.") }
When we run the above code and press the button we added, we get the following result.
There are some points to be noted −
Make sure that at least one account is added in the mail application on your iPhone.
Make sure you have the pdf stored locally on the address, which you want to upload.
- Related Articles
- How to send an attachment in email using Swift(ios)?
- How to send an email with a file attachment in Android using Kotlin?
- How to send an email with a file attachment in Android?
- How to send a email with attachment using a JSP page?
- How to send a file as an email attachment using the Linux command line?
- Sending an Attachment with email using Perl
- How to send email using PowerShell?
- How do we send an email using HTML forms?
- How to send HTML email using Android App?
- Send email using Java Program
- How to send HTML email using Android App using Kotlin?
- How to send Email on Android using JavaMail API?
- How to send a report through email using Selenium Webdriver?
- Send mail with attachment from your Gmail account using Python
- How to send a html based email using a JSP page?

Advertisements