Swift Send Email with MailGun

后端 未结 4 1684
时光说笑
时光说笑 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:40

    Swift 3 answer:

        func test() {
            let session = URLSession.shared
            var request = URLRequest(url: URL(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.data(using: .ascii)
            request.setValue("key-(Personal info)", forHTTPHeaderField: "api")
            let task = session.dataTask(with: 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! HTTPURLResponse
                    print("response code = \(httpResponse.statusCode)")
                }
    
    
            })
            task.resume()
        }
    

提交回复
热议问题