Add Email option to your app
This 'Emailer' class allows you to add a section to your own app so Customers can send emails back to you. You can use it for lots of different purposes like Customer Feedback buttons, etc.
An instance of this class should be created and stored in the view controller in which it is used. By creating an instance you make sure that all the buttons will continue working correctly as long as the view controller remains present.
We have also attached a raw file below to make it easier for you to view in Xcode.
Any questions on this class please feel free to contact us using the 'Contact' section of this website. Also, let us know if this is useful since we have 100s of other classes like this to make your life easier and can easily upload more.
An instance of this class should be created and stored in the view controller in which it is used. By creating an instance you make sure that all the buttons will continue working correctly as long as the view controller remains present.
We have also attached a raw file below to make it easier for you to view in Xcode.
Any questions on this class please feel free to contact us using the 'Contact' section of this website. Also, let us know if this is useful since we have 100s of other classes like this to make your life easier and can easily upload more.
Implementation Example
An example of this class's implementation can be seen in Swordy Quest, which is one of our Free iOS apps. To see the implementation simply download the app for free, then navigate to the 'More' Tab Bar and then select 'Email queries' to see what it can look like in your app:
The Emailer Class
Below is a raw format Swift file you can download plus the contents of the file are also written below to make things easier:
![](http://www.weebly.com/weebly/images/file_icons/file.png)
emailer.swift | |
File Size: | 2 kb |
File Type: | swift |
//
// Emailer.swift
//
// Copyright (c) 2015 Seligman Ventures Global Ltd. All rights reserved.
//
/*
Versions
--------
v1.0 Created
v1.1 Allow non-Html emails
v1.2 Allow email attachments (eg. images)
v1.3 Updated for Swift 3
v1.4 Added delegate to see result of email
v1.5 Updated implementation notes
v1.6 Updated implementation notes & important update on testing
Implementation
--------------
- Create new instance > set optional fields (e.g. subject) > show email
- Note: Emailer must be an instance variable (rather than a local variable) for 'cancel' button to work. Also create instance only once e.g. in viewDidLoad()
*/
import Foundation
import MessageUI
// Delegation
@objc protocol EmailerDelegate {
@objc optional func didFinishWithResult(result: MFMailComposeResult)
}
class Emailer : NSObject, MFMailComposeViewControllerDelegate {
// MARK: - Delegation
public var delegate: EmailerDelegate!
// MARK: - Properties
let parentViewController: UIViewController
var toEmails :[String] = []
var subject = ""
var body = ""
var attachmentType: String? // eg "image/png"
var attachmentData: NSData?
var image: UIImage?
// MARK: - Constructors
init(parentViewController: UIViewController) {
self.parentViewController = parentViewController
}
// MARK: - Public
func showEmail(isHtml: Bool) {
if MFMailComposeViewController.canSendMail() {
let picker = MFMailComposeViewController()
picker.mailComposeDelegate = self
picker.setToRecipients(toEmails)
picker.setSubject(subject)
picker.setMessageBody(body, isHTML: isHtml)
if attachmentType != nil {
picker.addAttachmentData(attachmentData! as Data, mimeType: attachmentType!, fileName: "Attachment1")
}
parentViewController.present(picker, animated: true, completion: nil)
}
else {
self.showSendMailErrorAlert()
}
}
// MARK: - MFMailComposeViewControllerDelegate
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Inform delegate
if self.delegate != nil {
self.delegate.didFinishWithResult!(result: result)
}
// Close view
controller.dismiss(animated: true, completion: nil)
}
// MARK: - Private
private func showSendMailErrorAlert() {
AlertFacade.showAlert(parentViewController: parentViewController,
title: "Could Not Send Email",
message: "Your device could not send e-mail. Please check e-mail configuration and try again.",
dismissButtonTitle: "OK")
}
}
// Emailer.swift
//
// Copyright (c) 2015 Seligman Ventures Global Ltd. All rights reserved.
//
/*
Versions
--------
v1.0 Created
v1.1 Allow non-Html emails
v1.2 Allow email attachments (eg. images)
v1.3 Updated for Swift 3
v1.4 Added delegate to see result of email
v1.5 Updated implementation notes
v1.6 Updated implementation notes & important update on testing
Implementation
--------------
- Create new instance > set optional fields (e.g. subject) > show email
- Note: Emailer must be an instance variable (rather than a local variable) for 'cancel' button to work. Also create instance only once e.g. in viewDidLoad()
*/
import Foundation
import MessageUI
// Delegation
@objc protocol EmailerDelegate {
@objc optional func didFinishWithResult(result: MFMailComposeResult)
}
class Emailer : NSObject, MFMailComposeViewControllerDelegate {
// MARK: - Delegation
public var delegate: EmailerDelegate!
// MARK: - Properties
let parentViewController: UIViewController
var toEmails :[String] = []
var subject = ""
var body = ""
var attachmentType: String? // eg "image/png"
var attachmentData: NSData?
var image: UIImage?
// MARK: - Constructors
init(parentViewController: UIViewController) {
self.parentViewController = parentViewController
}
// MARK: - Public
func showEmail(isHtml: Bool) {
if MFMailComposeViewController.canSendMail() {
let picker = MFMailComposeViewController()
picker.mailComposeDelegate = self
picker.setToRecipients(toEmails)
picker.setSubject(subject)
picker.setMessageBody(body, isHTML: isHtml)
if attachmentType != nil {
picker.addAttachmentData(attachmentData! as Data, mimeType: attachmentType!, fileName: "Attachment1")
}
parentViewController.present(picker, animated: true, completion: nil)
}
else {
self.showSendMailErrorAlert()
}
}
// MARK: - MFMailComposeViewControllerDelegate
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Inform delegate
if self.delegate != nil {
self.delegate.didFinishWithResult!(result: result)
}
// Close view
controller.dismiss(animated: true, completion: nil)
}
// MARK: - Private
private func showSendMailErrorAlert() {
AlertFacade.showAlert(parentViewController: parentViewController,
title: "Could Not Send Email",
message: "Your device could not send e-mail. Please check e-mail configuration and try again.",
dismissButtonTitle: "OK")
}
}