**Problem**

I would like to use the [MailGun](https://documentation.mailgun.com/quickstart-sending.html#how-to-start-sending-email) service to send emails from a pure Swift app.

**Research So Far**

As I understand it, [there are two methods to send an email](https://documentation.mailgun.com/quickstart-sending.html#send-via-smtp) via MailGun. One is to email MailGun with the emails, and MailGun will redirect it (See *Send via SMTP*). That will, as I understand it, not work, as iOS cannot programatically automatically send mail, and must use [methods that require user intervention](http://www.ioscreator.com/tutorials/send-email-tutorial-ios8-swift). As such, I should use the API directly. As I understand it, I need to open a URL to do this, and so I should use some form of `NSURLSession`, as per [this SO answer](https://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift)

**Code**

MailGun provides documentation for Python, which is as follows:

```
def send_simple_message():
return requests.post(
    "https://api.mailgun.net/v3/sandbox(Personal info).mailgun.org/messages",
    auth=("api", "key-(Personal info)"),
    data={"from": "Excited User <(Personal info)>",
          "to": ["bar@example.com", "(Personal info)"],
          "subject": "Hello",
          "text": "Testing some Mailgun awesomness!"})

```

with (Personal info) being substituted for keys/information/emails.

**Question**

How do I do that in Swift?

Thanks!
['https://Stackoverflow.com/questions/33325137', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3892545/']
In python, the `auth` is being passed in the header. 

You have to do a http post request, passing both the header and the body.

This is a working code:

```
func test() {
        let session = NSURLSession.sharedSession()
        let request = NSMutableURLRequest(URL: NSURL(string: "https://api.mailgun.net/v3/sandbox(Personal info).mailgun.org/messages")!)
        request.HTTPMethod = "POST"
        let data = "from: Excited User <(Personal info)>&to: [bar@example.com,(Personal info)]&subject:Hello&text:Testinggsome Mailgun awesomness!"
        request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)
        request.setValue("key-(Personal info)", forHTTPHeaderField: "api")
        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

            if let error = error {
                print(error)
            }
            if let response = response {
                print("url = \(response.URL!)")
                print("response = \(response)")
                let httpResponse = response as! NSHTTPURLResponse
                print("response code = \(httpResponse.statusCode)")
            }

        })
        task.resume()
    }

```
[`requests.post`](http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests) sends an HTTP POST request, encoding key/value pairs as `application/x-www-form-urlencoded`. You need to do the same.

* convert the set of key-value pairs into `application/x-www-form-urlencoded` as per [How to escape the HTTP params in Swift](https://stackoverflow.com/questions/27148100/how-to-escape-the-http-params-in-swift)
* compose the request using the resulting string for data & send it as per [iOS : http Post using swift](https://stackoverflow.com/questions/30596114/ios-http-post-using-swift)