gpt4 book ai didi

ios - 在 Swift 的 MailJet 电子邮件中附加 PDF

转载 作者:行者123 更新时间:2023-11-28 13:49:15 26 4
gpt4 key购买 nike

我正在根据以下代码从 html 字符串生成 PDF 数据:https://gist.github.com/nyg/b8cd742250826cb1471f然后我得到 pdfData 并这样做:

let base64Data = pdfData.base64EncodedString()
let email = "email@gmail.com"

var recipients = [Any]()
recipients.append(["Email": email])

let body: [String: Any] = [
"FromEmail": "<...>@gmail.com",
"FromName": "<...>",
"Subject": "<...>",
"Text-part": "<...>",
"Recipients": recipients,
"Attachments": [
"Content-type": "application/pdf",
"Filename": "file.pdf",
"content": base64Data
]
]

let username_key = "username_key"
let password_key = "password_key"
let loginString = NSString(format: "%@:%@", username_key, password_key)
let loginData = loginString.data(using: String.Encoding.utf8.rawValue)!
let base64LoginString = loginData.base64EncodedString()

var request = URLRequest(url: URL(string: "https://api.mailjet.com/v3/send")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Basic <\(base64LoginString)>", forHTTPHeaderField: "Authorization")

do {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
}
catch {
print("Error during JSON Serialization")
return
}

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in

if error == nil {
self.dismiss(animated: true, completion: nil)
} else {
// Show error alert
}
})
task.resume()

代码有效,我也在我的应用程序的其他 View 中使用它。问题是发送的电子邮件没有附件。你能看出问题出在哪里吗?我也试过在 base64 中直接编码 html 字符串,然后将其作为 .html 文件附加到电子邮件中,但邮件仍然没有附件。

最佳答案

如果我没看错,您的“附件”结构有误。在您的代码中,它是一个字典,其中包含三个键值对,分别代表附件的类型、名称和内容。但它应该是包含这三个键值对的对象列表。

您还为内容类型和内容命名了错误的键。 “内容类型”需要是“ContentType”,“内容”需要是“Base64Content”。

在这里您可以看到 mailjet 文档中的一个工作示例:

"Attachments": [
{
"ContentType": "text/plain",
"Filename": "test.txt",
"Base64Content": "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"
}
]

所以也许你需要改变你的 body :

let body: [String: Any] = [
"FromEmail": "<...>@gmail.com",
"FromName": "<...>",
"Subject": "<...>",
"Text-part": "<...>",
"Recipients": recipients,
"Attachments": [
[
"ContentType": "application/pdf",
"Filename": "file.pdf",
"Base64Content": base64Data
]
]
]

希望能帮到你!

关于ios - 在 Swift 的 MailJet 电子邮件中附加 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948881/

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