Swift Send Email with MailGun

后端 未结 4 1647
时光说笑
时光说笑 2021-01-15 01:20

Problem

I would like to use the MailGun service to send emails from a pure Swift app.

Research So Far

As I understa

4条回答
  •  [愿得一人]
    2021-01-15 01:50

    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()
        }
    

提交回复
热议问题