I'm generating a PDF data from an html String based on this code: https://gist.github.com/nyg/b8cd742250826cb1471f
I'm then getting pdfData
and doing this:
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()
The code is working, I'm using it also in other views of my app. The problem is that the email is sent without an attachment. Can you see where the problem is? I've also tried to encode directly the html string in base64 and then attach it as a .html file to the email, but the message still comes without attachments.