phpguzzlepostmark

Postmark/Guzzle InvalidArgumentException: Invalid resource type: boolean


I keep getting an InvalidArgumentException: Invalid resource type: boolean from some emails being sent via Postmark. Some are working, others aren't. I don't see anything different between the emails that are working and those that are having errors. They are running the same code with the same params.

Here's what I'm passing into Postmark:

$client = new PostmarkClient($config->postmark->token);
$sendResult = $client->sendEmail(
  $config->postmark->sender,
  $recipient,
  $subject,
  $html_body,
  $text_body,
  null, true, null, null, null, null, $attachments
);

Here's the call stack:

vendor/guzzlehttp/streams/src/Stream.php in factory at line 85
throw new \InvalidArgumentException('Invalid resource type: ' . $type);

vendor/guzzlehttp/guzzle/src/Message/MessageFactory.php in applyOptions at line 345
$request->setBody(Stream::factory(json_encode($value)));

vendor/guzzlehttp/guzzle/src/Message/MessageFactory.php in createRequest at line 98
$this->applyOptions($request, $options);

vendor/guzzlehttp/guzzle/src/Client.php in createRequest at line 120
return $this->messageFactory->createRequest($method, $url, $options);

vendor/wildbit/postmark-php/src/Postmark/PostmarkClientBase.php in processRestRequest at line 101
$request = $client->createRequest($method, $url, $options);

vendor/wildbit/postmark-php/src/Postmark/PostmarkClient.php in sendEmail at line 61
return new DynamicResponseModel($this->processRestRequest('POST', '/email', $body));

library/send_mail.php in send_mail at line 86

I've googled for a while and haven't found anything to make sense of this.

I've looked here: https://laracasts.com/discuss/channels/general-discussion/mandrill-trouble-invalid-resource-type-boolean and here: https://laracasts.com/forum/?p=2325-problems-using-mandrill/0 but both of those were having issues with Laravel+Mandrill, but I'm using Postmark and no framework. We do, however, have Guzzle in common. Both of those posts said that they handled it by updating or reinstalling their Guzzle.

I seem to be running a "working" version of Guzzle. GitHub Issue #628 for Guzzle seems to be exactly what I'm seeing, but that issue says it was fixed in 4.0.2, I'm on Guzzle 5.3:

From my composer.lock on the server:

{
  "name": "guzzlehttp/guzzle",
  "version": "5.3.0"
},
...
{
  "name": "guzzlehttp/streams",
  "version": "3.0.0"
},
...
{
  "name": "wildbit/postmark-php",
  "version": "dev-master",
  "source": {
      "type": "git",
      "url": "https://github.com/wildbit/postmark-php",
      "reference": "2eabc627c3f2a693b986e51d2f892cc2e2d065b3"
  },
  "require": {
    "guzzlehttp/guzzle": "~5.1",
    "php": ">=5.4.0"
  },
}

Any thoughts?


Solution

  • When your error isn't immediately obvious, then call stak will usually shed some light on what is going wrong. This is no exception and I should have noticed it sooner.

    The second to last item in the call stack is where this went bad.

    $request->setBody(Stream::factory(json_encode($value)));
    

    Here, json_encode($value) is returning a boolean. Using json_last_error() I was able to determine that the problem was with mixed character encoding (JSON_ERROR_UTF8).

    Somewhere in the user input there was user-provided data that was a mix of UTF-8 and ISO-8895-1. Since this was inconsistent, some emails would work just fine, while others were failing.

    I ended up forcing my $html_body and $text_body to be UTF-8 and things started working.