I got my email working fine. The only thing i need to do is add an additional string to my message body. For example, I want to add Name: and textfield to my message body. Something like this.
Name: John Smith
Phone: 566-654-6577
Email: [email protected]
Right now, the message body only show the following.
John Smith
566-654-6577
import UIKit
import MessageUI
class EmailTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var phone: UITextField!
@IBOutlet weak var email: UITextField!
func appendTextFromTextField(string: String, textField: UITextField) -> String {
return string + textField.text + "\n \n"
}
@IBAction func SendEmailButton(sender: AnyObject) {
var fields: [UITextField] = [name, phone, email]
var messageBody = ""
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
var emailTitle = "Interface Information"
var toRecipents = [""]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: %@", [error.localizedDescription])
default:
break
}
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Remove the for
loop and try replacing your method with this one:
@IBAction func sendEmailButton(sender: UIButton) {
var messageBody = "Name:\(name.text)\nPhone:\(phone.text)\nEmail:\(email.text) "
var emailTitle = "Interface Information"
var toRecipents = [""]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}