SparkPost/php-sparkpost

Form feed replacement is making our JSON invalid and causing a 422 error

Closed this issue · 5 comments

When sending this HTML email using the latest version of this library, the API will consistently return a 422 Unprocessable Entity error response:

<!DOCTYPE html>
<html>
    <head>
        <style>
            .foo {
                content: "\f00c";
                font-family: "Font Awesome 5 Pro";
            }
        </style>
    </head>
    <body>
        bar
    </body>
</html>

It seems the JSON is being garbled by a string replace in the library, just before it's sent to the SparkPost API.

// Valid JSON, before string modification
{"content":{"from":{"name":"Foo Bar","email":"foo@bar.com"},"subject":"Test email","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <style>\n            .foo {\n                content: \"\\f00c\";\n                font-family: \"Font Awesome 5 Pro\";\n            }\n        <\/style>\n    <\/head>\n    <body>\n        bar\n    <\/body>\n<\/html>","text":"Test subject","headers":null},"options":{"open_tracking":true,"click_tracking":true,"transactional":true,"ip_pool":"medium"},"recipients":[{"address":{"name":"Foo Bar","email":"foo@bar.com"}}],"metadata":null}

// Invalid JSON, after string modification
{"content":{"from":{"name":"Foo Bar","email":"foo@bar.com"},"subject":"Test email","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <style>\n            .foo {\n                content: \"\00c\";\n                font-family: \"Font Awesome 5 Pro\";\n            }\n        <\/style>\n    <\/head>\n    <body>\n        bar\n    <\/body>\n<\/html>","text":"Test subject","headers":null},"options":{"open_tracking":true,"click_tracking":true,"transactional":true,"ip_pool":"medium"},"recipients":[{"address":{"name":"Foo Bar","email":"foo@bar.com"}}],"metadata":null}

If we remove the string replace the email goes through OK. I understand that this is in place to protect against form feeds from being sent to the SparkPost API, but in our case this is valid HTML (not a form feed character) and the SparkPost API has no issue with it. In our case, the library is replacing good JSON with bad.

Hi @jonathanbull I've been looking at this to try and help you on my own initiative, but am struggling to get even a simple app to run in order to verify the change.

The standard README install instructions gave dependency errors on Guzzle6, so based on a comment here I'm using

composer require php-http/guzzle6-adapter "^1.1"
composer require guzzlehttp/guzzle "^6.0"
composer require sparkpost/sparkpost

and can build a simple mail sending app to reproduce what you're seeing. Unfortunately this runs, creates the SparkPost object, then gives

Exception has occurred.
Http\Discovery\Exception\PuliUnavailableException: Puli Factory is not available

Update: looks like this is just a feature of how the Guzzle stack starts up. The exception gets caught and carries on. I can now send a mail and reproduce the issue you're seeing.

Glad you were able to figure out those Guzzle issues @tuck1s. We've been doing some more testing on our end and came to the conclusion as you – the SparkPost API seems to accept form feed characters just fine!

Thanks! Just waiting for reviewers to OK, then I'll merge #198. There are a few other issues in the repo (Travis CI etc) which we should fix but in a separate PR.

Merged. 2.2.1 release done.

Thank you for the quick turnaround @tuck1s