2015-03-10 23 views
40

Bir uygulamada hızlıca e-posta gönderirsiniz. Örneğin, kullanıcınızın Parse (veya değil) ile bir sosyal medya uygulamasında parolalarını sıfırlamasını istemek gibi, ancak otomatik olmasını istediğiniz için MessageUI kullanmıyorsunuz. Bazı araştırmalar yaptım ve posta tenekesi ile mümkün olabileceğini öğrendim ama hızlı ve XCode 6 ile nasıl kullanılacağını anlayamıyorum. Lütfen bana yardım edebilir misiniz?Swift ile e-posta gönderme

+0

Ne denediniz? – Loko

cevap

86

Elbette yapabilirsin.

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(["[email protected]"]) 
     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) 

    } 
} 

Kaynak Andrew Bancroft

+9

sorudan: "ama IOS9.2 simulatörü kullanırken otomatik olarak" –

+0

otomatik olmasını istiyorsanız MessageUI kullanmıyorsunuz, görünüm "viewServiceDidTerminateWithError" mesajıyla kilitleniyor. Ancak diğer sorular bunun sadece simülatörle ilgili problem olduğunu ve çözümün gerçek cihazlarda çalışacağını belirtir. – Dashrath

+6

Looks _very_ tanıdık https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller- with-swift/ – n13

6

Ayrıştırma kutudan çıktığı Mailgun ve Mandrill destekler. Bkz. documentation

Bir CloudCode işlevi yazmanız, ardından uygulamanızdan çağırmanız gerekir.

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

Mailgun kullanarak posta göndermek için örnek kod parçacıkları.

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

Mailgun.sendEmail({ 
    to: "[email protected]", 
    from: "[email protected]", 
    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"); 
    } 
}); 
+0

İçe aktarmam gereken bir modül var mı? Bana hata veriyor –

+0

var Mailgun = required ('mailgun'); – picciano

+0

Ben de aynı kodu var ve hala hataları alıyorum da hızlı bir şekilde " –