jbono
jbono

Reputation: 105

Send email from iOS app

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

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?

Upvotes: 3

Views: 1891

Answers (2)

Krunal
Krunal

Reputation: 79776

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.

Upvotes: 1

AamirR
AamirR

Reputation: 12218

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": "[email protected]"
    ]
    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("[email protected]", recipientName: "Recipient Name", subject: "PlainText", text: "This is a PlainText test email.")

Usage-2 (html):

sendEmail("[email protected]", recipientName: "", subject: "HTML Test", text: "<html><div>This is a <b>HTML</b> test email.</div></html>")

Upvotes: 2

Related Questions