0

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.

Francesco
  • 169
  • 1
  • 1
  • 14

1 Answers1

1

If I see correctly, your "Attachments" has the wrong structure. In your code it is one dictionary containing three key-value-pairs for type, name and content of your attachment. But it should be a list of objects containing these three key-value-pairs.

Also you named the keys for content type and content wrong. "Content-type" needs to be "ContentType" and "content" needs to be "Base64Content".

Here you can see a working example from the mailjet documentation:

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

So maybe you need to change your body to:

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

I hope I could help!