3

I'm building a very basic iOS app with Swift 3. It consists on a formulary with this fields:

  • Name
  • Last name
  • Email
  • Phone
  • City (Picker View)
  • Description

I want to send this data with an email. Using field Email as a receiver, and the rest of the fields in the Body.

I want the sender always be the same, I have a Wordpress backend, I don't know if I must have endpoint to do this (maybe sending mail with PHP, not from the App directly).

I tried to use MFMailComposeViewController, but this open the modal to send an email, and requires to configure an email account on the device.

Any idea how to do this?

Krunal
  • 77,632
  • 48
  • 245
  • 261
jbono
  • 105
  • 1
  • 9
  • you either need a webservice endpoint to do this otherwise you have to configure email account in iPhone and use that – Shabir jan May 16 '17 at 10:23
  • You need to design service which takes data form iOS and in backend use any mailchip service to send mail through same sender. – CodeChanger May 16 '17 at 10:24

2 Answers2

2

You need a service to deliver your emails, this can be your own WebService or you can choose one of the many available services, like sendgrid.com which is fairly easy to implement in your Swift app and has a 40k email limit for free.

Here is a Swift 3 example using sendgrid.com service:

Note: before you use this method, signup at sendgrid.com to get the api_user and api_key values.

func sendEmail(_ email: String, recipientName: String, subject: String, text: String) {
    let params = [
        "api_user": ENTER_YOUR_API_USER,
        "api_key": HERE_YOU_ENTER_API_KEY,
        "to": email,
        "toname": recipientName,
        "subject": subject,
        "html": text,
        "from": "noreply@example.com"
    ]
    var parts: [String] = []
    for (k, v) in params {
        let key = String(describing: k).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
        let value = String(describing: v).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
        parts.append(String(format: "%@=%@", key!, value!))
    }
    guard let url = URL(string: String(format: "%@?%@", "https://api.sendgrid.com/api/mail.send.json", parts.joined(separator: "&"))) else { return }

    let session = URLSession(configuration: .default, delegate: nil, delegateQueue: nil)
    let task = session.dataTask(with: url, completionHandler: {
        (data, response, error) in
        if (error == nil) {
            print("Email delivered!")
        } else {
            print("Email could not be delivered!")
        }
    })
    task.resume()
    session.finishTasksAndInvalidate()
}

Usage-1 (plain/text):

sendEmail("recipient@email.com", recipientName: "Recipient Name", subject: "PlainText", text: "This is a PlainText test email.")

Usage-2 (html):

sendEmail("recipient@email.com", recipientName: "", subject: "HTML Test", text: "<html><div>This is a <b>HTML</b> test email.</div></html>")
AamirR
  • 11,672
  • 4
  • 59
  • 73
  • Where can I find the API_USER in SendGrid? –  Jul 03 '19 at 22:32
  • API_USER would be your username, API_KEY your password, check out the api v2 documentation https://sendgrid.com/docs/API_Reference/Web_API/mail.html – AamirR Jul 04 '19 at 09:38
  • This does not appear to work. Code was copied and even with "email delivered" there was no email delivered. – dub Dec 31 '20 at 06:07
1

Send your data to your web server using web service and from there send an e-mail to Receiver/Recipients. Web server can send e-mail without notifying (mobile app) user.

You can setup an account on web server that can send all e-mails from a single account. In your case, sending emails from web server using web service would be best choice.

iOS won't allow to send an e-mail without using MFMailComposeViewController.

Krunal
  • 77,632
  • 48
  • 245
  • 261