1

I do some research and testing, but got no working solution so far. I hope someone could help me :)

My objective: I want to use mailgun (or something similar?) for transactional email within an ios app.

What i've done so far: I've tested the curl-statement with success:

    curl -s --user 'api:(myAPIKey)' \
    https://api.mailgun.net/v3/sandbox(mySandBoxAddressKey).mailgun.org/messages \
    -F from='Excited User <postmaster@sandbox(mySandBoxAddressKey).mailgun.org>' \
    -F to='(myPersonalMailAddress)@gmail.com' \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomeness!'

In Swift i've tried the same with

    let session = URLSession.shared
    let url = URL(string: "https://api.mailgun.net/v3/sandbox(mySandBoxAddressKey).mailgun.org/messages")!
    var request = URLRequest(url: url)

    let credentials = "api:(myAPIKey)"
    request.setValue("Basic \(Data(credentials.utf8).base64EncodedString())", forHTTPHeaderField: "Authorization")

    let data = "from: Mailgun Sandbox <(postmaster@sandbox(mySandBoxAddressKey).mailgun.org)>&to: [(myPersonalMailAddress)@gmail.com,((myPersonalMailAddress)@gmail.com)]&subject:Hello&text:Congratulations"
    request.httpBody = data.data(using: String.Encoding.ascii)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
        if let error = error {
            print(error)
        }
        if let response = response {
            print("mimeType = \(response.mimeType)")
            print("url = \(response.url!)")
            print("response = \(response)")
            let httpResponse = response as! HTTPURLResponse
            print("response code = \(httpResponse.statusCode)")
            print("allHeaderFields = \(httpResponse.allHeaderFields)")
        }
    })
    task.resume()

I'll always got a response 400:

response = <NSHTTPURLResponse: 0x600000430020> { URL: https://api.mailgun.net/v3/sandbox(mySandBoxAddressKey).mailgun.org/messages } { Status Code: 400, Headers {
Connection =     (
    "keep-alive"
);
"Content-Length" =     (
    86
);
"Content-Type" =     (
    "application/json"
);
Date =     (
    "Wed, 06 Jun 2018 15:17:53 GMT"
);
Server =     (
    nginx
);
} }

I thought it is a problem with the body and tried to vary the "data"-String. I don't know if it's the right part in my code. Last but not least, i've tried to setup the httpBody-Part with a json:

let json: [String: Any] = ["from": "Mailgun Sandbox <(postmaster@sandbox(mySandBoxAddressKey).mailgun.org)>", "to": "(myPersonalMailAddress)@gmail.com", "subject": "test", "text": "Nochmal test"]
    let jsonData = try? JSONSerialization.data(withJSONObject: json)
    request.httpBody = jsonData

I also read this SO-articles: Swift Send Email with MailGun , 401 Forbidden when sending email with Swift and Mailgun , How to send emails in Swift using Mailgun

andre_hold
  • 562
  • 8
  • 20
  • An HTTP 400 is a bad request. I'd inspect your request payload right before sending it and compare it to what the API expects – James Parsons Jun 06 '18 at 15:55
  • @James_Parsons thank you. i'll take a look at the macOs console and the xcode debugging area/console but couldn't found any more details. where else could i find the payload? many thanks – andre_hold Jun 08 '18 at 08:25

0 Answers0