42

how would you send an email with swift in an app. Like say for example your user want's to reset their password in a social media application with Parse(or not), but you don't wan't to use MessageUI because you want it to be automatic. I've done some research and found out that it can be possible with mailgun but i cannot figure out how to use it with swift and XCode 6. Can you please help me?

picciano
  • 22,341
  • 9
  • 69
  • 82
Noah Barsky
  • 507
  • 1
  • 5
  • 5

2 Answers2

88

Sure you can.

import Foundation
import UIKit
import MessageUI

class ViewController: ViewController,MFMailComposeViewControllerDelegate {

    @IBAction func sendEmailButtonTapped(sender: AnyObject) {
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert()
        }
    }

    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

        mailComposerVC.setToRecipients(["nurdin@gmail.com"])
        mailComposerVC.setSubject("Sending you an in-app e-mail...")
        mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

        return mailComposerVC
    }

    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    }

    // MARK: MFMailComposeViewControllerDelegate

    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)

    }
}

Source Andrew Bancroft

Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • 12
    from the question: "but you don't wan't to use MessageUI because you want it to be automatic" – Matthias Bauch Mar 10 '15 at 12:47
  • when using In IOS9.2 simulator ,the view crashes with message "viewServiceDidTerminateWithError" . However other questions states that this is problem with simulator only and solution will works in real devices – Dashrath Jan 14 '16 at 12:06
  • 7
    Looks _very_ familiar https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/ – n13 Feb 09 '16 at 15:42
7

Parse supports both Mailgun and Mandrill out of the box. See their documentation

You will need to write a CloudCode function, then call it from your app.

PFCloud.callFunctionInBackground("hello", withParameters:[:]) {
  (result: AnyObject!, error: NSError!) -> Void in
  if error == nil {
    // result is "Hello world!"
  }
}

Example code snippets to send mail using Mailgun.

var Mailgun = require('mailgun');
Mailgun.initialize('myDomainName', 'myAPIKey');

Mailgun.sendEmail({
  to: "email@example.com",
  from: "Mailgun@CloudCode.com",
  subject: "Hello from Cloud Code!",
  text: "Using Parse and Mailgun is great!"
}, {
  success: function(httpResponse) {
    console.log(httpResponse);
    response.success("Email sent!");
  },
  error: function(httpResponse) {
    console.error(httpResponse);
    response.error("Uh oh, something went wrong");
  }
});
picciano
  • 22,341
  • 9
  • 69
  • 82
  • Is there a module i need to import? it is giving me errors – Noah Barsky Mar 12 '15 at 06:22
  • var Mailgun = require('mailgun'); – picciano Mar 12 '15 at 16:55
  • I have that same code and i am still getting errors also in swift you have to use "" for a string instead of ' – Noah Barsky Mar 13 '15 at 09:00
  • No. This is a CloudCode function using Parse.com. It is a JavaScript-based language. Your question mentioned also using Parse, which is why I suggested this as an alternative. You could use Mailgun directly from your Swift code as well, but this will maintain better control over your API keys. – picciano Mar 13 '15 at 13:55
  • This is Javascript code. – Unome Apr 15 '15 at 03:05
  • 1
    Is there any good documentation on how to integrate Mailgun with swift? @picciano – Unome Apr 15 '15 at 03:50