0

I'm trying to get my app to send an email containing all the data collected in a View controller

here's my code

func sendemail() {
    let formatter = NSDateFormatter()
    formatter.dateStyle = .MediumStyle
    formatter.timeStyle = .FullStyle
    let dateString = formatter.stringFromDate(datePicker.date)

    var emailtext = NSLocalizedString("Pilot name : ", comment: "");
    emailtext += String(pilotsTextField.text)
    emailtext += "<br />";
    emailtext = NSLocalizedString("Aicraft : C-", comment: "");
    emailtext += String(acRegTextField.text)
    emailtext += "<br />";
    emailtext += "<br />";
    emailtext = NSLocalizedString("Passengers :", comment: "");
    emailtext += "<br />";
    for (var i=0 ; i < passengers.count; i++) {
                   let passenger = passengers[i]
        emailtext = "Name: \(passenger.paxName)"
        emailtext += "<br />";
        emailtext = "Name: \(passenger.paxWeight)"
        emailtext += "<br />";
        emailtext = "Name: \(passenger.paxEmergencyName)"
        emailtext += "<br />";
        emailtext = "Name: \(passenger.paxEmergencyPhone)"
        emailtext += "<br />";
        emailtext = "Name: \(passenger.paxDestinationComments)"
        emailtext += "<br />";
        emailtext += "<br />";
    }
    emailtext += "<br />";
    emailtext += "Date and Departure time : ";
    emailtext += dateString;
    emailtext += "<br />";
    emailtext += NSLocalizedString("Canadian Helicopters Limited", comment: "");

    let mailVC = MFMailComposeViewController()
    mailVC.mailComposeDelegate = self
    mailVC.setToRecipients(["manifests@canadianhelicopters.com"])
    mailVC.setSubject(NSLocalizedString("Manifest from \(pilotsTextField.text) for \(acRegTextField.text)", comment: "email"))
    mailVC.setMessageBody(("\(emailtext)"), isHTML: true)

    self.presentViewController(mailVC, animated:true, completion:nil)
}

However, everything found in the loop does not populate the email form...

any ideas ???

Jp4Real
  • 1,982
  • 4
  • 18
  • 33
  • 2
    From `UITableView` ? There is no reference to UITableView in your code, no? Aren't you missing some "+" when you do `emailText =` instead of `emailText+=`? – Larme Mar 21 '16 at 16:04
  • @Larme is right but you should see everything sarting from `"Name: \(passenger.pasDestinationComments)"` or from localized `"Passengers:"` respectively. However, this would do but does not solve your problem: `mailVC.setMessageBody(emailtext, isHTML: true)`. Well, I am not totally sure. It may even solve your problem because by doing so you'd get rid of the additional and redundant set of brackets in this very statement. – Hermann Klecker Mar 21 '16 at 16:41
  • BTW, you are not using `dateString' at all. – Hermann Klecker Mar 21 '16 at 16:44
  • datString is used at the end, thing is I'm us gin this "Email Text" to create a complete text var that i can't easily translate and modify, then I put it in the setMessageBody – Jp4Real Mar 21 '16 at 16:59
  • btw the element that is in the UItableview is an array of passengers – Jp4Real Mar 21 '16 at 17:05
  • @Larme I don't know why the website formmating did that, because in my code only the 1st emailText has an = and the rest have += – Jp4Real Mar 21 '16 at 17:21
  • My problem really is that anything insede the passenger.count loop does not show in the email. the rest is fine – Jp4Real Mar 21 '16 at 17:22
  • Are the value `let passenger = passengers[i]` correct? What's the value of `passengers.count`? What's the value of `emailtext` whenever you add one passenger info? – Larme Mar 21 '16 at 17:23
  • depending on how many passengers you have, but in my test `passengers.count` should return 2. so I put in this loop to loop through `passengers` until there is none left. then write all of them in the `emailText` var to send it via email. value of `emailText` shoul be equal to all the passengers and the data of each of them (Name, phone number etc....) – Jp4Real Mar 21 '16 at 17:30
  • My question is: Is `emailtext += "Name: \(passenger.paxName)"` added really to `emailtext`? `passenger.paxName` has a value? If you do instead `emailtext += "333"` do you get "333" (each time for each passenger) in `emailtext`? – Larme Mar 21 '16 at 17:41
  • good question let me try – Jp4Real Mar 21 '16 at 17:58

1 Answers1

1
func sendemail() {
    let formatter = NSDateFormatter()
    formatter.dateStyle = .MediumStyle
    formatter.timeStyle = .FullStyle
    let dateString = formatter.stringFromDate(datePicker.date)

    var emailtext = NSLocalizedString("Pilot name : ", comment: "");
    emailtext += String(pilotsTextField.text)
    emailtext += "<br />";
    emailtext += NSLocalizedString("Aicraft : C-", comment: "");
    emailtext += String(acRegTextField.text)
    emailtext += "<br />";
    emailtext += "<br />";
    emailtext += NSLocalizedString("Passengers :", comment: "");
    emailtext += "<br />";
    for (var i=0 ; i < passengers.count; i++) {
                   let passenger = passengers[i]
        emailtext += "Name: \(passenger.paxName)"
        emailtext += "<br />";
        emailtext += "Name: \(passenger.paxWeight)"
        emailtext += "<br />";
        emailtext += "Name: \(passenger.paxEmergencyName)"
        emailtext += "<br />";
        emailtext += "Name: \(passenger.paxEmergencyPhone)"
        emailtext += "<br />";
        emailtext += "Name: \(passenger.paxDestinationComments)"
        emailtext += "<br />";
        emailtext += "<br />";
    }
    emailtext += "<br />";
    emailtext += "Date and Departure time : ";
    emailtext += dateString;
    emailtext += "<br />";
    emailtext += NSLocalizedString("Canadian Helicopters Limited", comment: "");

    let mailVC = MFMailComposeViewController()
    mailVC.mailComposeDelegate = self
    mailVC.setToRecipients(["manifests@canadianhelicopters.com"])
    mailVC.setSubject(NSLocalizedString("Manifest from \(pilotsTextField.text) for \(acRegTextField.text)", comment: "email"))
    mailVC.setMessageBody(emailtext, isHTML: true)

    self.presentViewController(mailVC, animated:true, completion:nil)
}
Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • beside adding the + before the = there is no change in the code ? my problem is related to the loop, anything inside `for (var i=0 ; i < passengers.count; i++) {` is not executed, so not written anywhere in the mailComposer – Jp4Real Mar 21 '16 at 17:26
  • There is more change in this line: `mailVC.setMessageBody(emailtext, isHTML: true)` – Hermann Klecker Mar 22 '16 at 09:11