2017-11-17 13 views
0

私はMailgunを使用して電子メールを送信するコードを持っていますが、電子メールが正常に送信されたという応答コード200が返ってきます。いずれかを表示する。Swift - Mailgun - 電子メールを受信しない

以下

私のコードです:私はそれを下に、データ変数にいくつかの微調整を行うことにより、作業しまった

func email() { 
    let session = URLSession.shared 
    let request = NSMutableURLRequest(url: NSURL(string: "https://api.mailgun.net/v3/sandboxBLAHBLAH.mailgun.org")! as URL) 

    request.httpMethod = "POST" 
    let credentials = "api:key-BLAHBLAH" 
    request.setValue("Basic \(credentials.toBase64())", forHTTPHeaderField: "Authorization") 

    let data = "from:Test<([email protected])>&to:[[email protected],([email protected])]&subject:Hello&text:Testing_email" 

    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("url = \(response.url!)") 
      print("response = \(response)") 
      let httpResponse = response as! HTTPURLResponse 
      print("response code = \(httpResponse.statusCode)") 
     } 
    }) 
    task.resume() 
} 

extension String { 

    func fromBase64() -> String? { 
     guard let data = Data(base64Encoded: self) else { 
      return nil 
     } 

     return String(data: data, encoding: .utf8) 
    } 

    func toBase64() -> String { 
     return Data(self.utf8).base64EncodedString() 
    } 
} 

答えて

0

は私のために動作するコードは次のとおりです。

FUNCメール(){ LET要求:NSMutableURLRequest = NSMutableURLRequest(URL:NSURL(文字列:! "https://api.mailgun.net/v3/sandbox {YOURS WITH REPLACE} .mailgun.org /メッセージ")URLなど) request.httpMethod = "POST"

 // Basic Authentication 
    let username = "api" 
    let password = "key-{REPLACE WITH YOURS}" 
    let loginString = NSString(format: "%@:%@", username, password) 
    let loginData: NSData = loginString.data(using: String.Encoding.utf8.rawValue)! as NSData 
    let base64LoginString = loginData.base64EncodedString(options: []) 
    request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization") 

    let bodyStr = "from=Mailgun Sandbox <[email protected]{REPLACE WITH YOURS}.mailgun.org>&to=Receiver name <[email protected]>&subject=Test&text=thank you!" 

    // appending the data 
    request.httpBody = bodyStr.data(using: String.Encoding.utf8); 

    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in 
    // ... do other stuff here 
    }) 

    task.resume() 
} 
関連する問題