Advertisement
Milotronik

iOS send email from app

Jan 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.15 KB | None | 0 0
  1.     let email = "feedback@company.com"
  2.     let subject = "subject"
  3.     let bodyText = "Please provide information that will help us to serve you better"
  4.     if MFMailComposeViewController.canSendMail() {
  5.         let mailComposerVC = MFMailComposeViewController()
  6.         mailComposerVC.mailComposeDelegate = self
  7.         mailComposerVC.setToRecipients([email])
  8.         mailComposerVC.setSubject(subject)
  9.         mailComposerVC.setMessageBody(bodyText, isHTML: true)
  10.         self.present(mailComposerVC, animated: true, completion: nil)
  11.     } else {
  12.         let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  13.         if let emailURL = URL(string: coded!)
  14.         {
  15.             if UIApplication.shared.canOpenURL(emailURL)
  16.             {
  17.                 UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
  18.                     if !result {
  19.                         // show some Toast or error alert
  20.                         //("Your device is not currently configured to send mail.")
  21.                     }
  22.                 })
  23.             }
  24.         }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement