gpt4 book ai didi

ios - Swift - Mailgun - 未收到电子邮件

转载 作者:行者123 更新时间:2023-11-30 12:00:10 25 4
gpt4 key购买 nike

我有下面的代码,它使用 Mailgun 发送电子邮件,我收到响应代码 200,这应该意味着电子邮件已成功发送,但是我没有收到任何电子邮件,我的 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<(postmaster@BLAHBLAH.mailgun.org)>&to:[BLAH@gmail.com,(BLAH@gmail.com)]&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()
}
}

最佳答案

我通过对数据变量进行一些调整来使其工作,下面是我的工作代码:

函数电子邮件(){ 让请求: NSMutableURLRequest = NSMutableURLRequest(url: NSURL(string: "https://api.mailgun.net/v3/sandbox {REPLACE With YOURS}.mailgun.org/messages")! 作为 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 <postmaster@sandbox{REPLACE WITH YOURS}.mailgun.org>&to=Receiver name <SOMEEMAIL@SOMEMEAIL.com>&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()
}

关于ios - Swift - Mailgun - 未收到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47344971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com