mailgun-php: Can't send binary attachments through cURL
Hello, I’ve been trying to send mails with PDF or JPG attachments but it fails in the cURL command formatter because escapeshellarg receives the file binary contents:
Stacktrace:
Symfony\Component\Debug\Exception\FatalErrorException:
Error: escapeshellarg(): Input string contains NULL bytes
at vendor/php-http/message/src/Formatter/CurlCommandFormatter.php:43
CurlCommandFormatter:
$body = $request->getBody();
if ($body->getSize() > 0) {
if (!$body->isSeekable()) {
return 'Cant format Request as cUrl command if body stream is not seekable.';
}
$command .= sprintf(' --data %s', escapeshellarg($body->__toString()));
$body->rewind();
}
This is how I initialize the attachment parameter:
$param['attachment'] = array();
foreach ($attachments as $attachment) {
$filename = $attachment['fileName'];
$fileContent = $attachment['content']; // file_get_contents of PDF file made with wkhtmltopdf
$tmpPath = '/tmp/' .$filename;
file_put_contents($tmpPath, $fileContent);
array_push($param['attachment'], array(
'filePath' => $tmpPath,
'filename' => $filename,
));
}
When the call to escapeshellarg is removed, it works as expected and my files are attached.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (2 by maintainers)
Hi all,
I have tested it and I am unable to replicate the same error. The steps I followed:
These are the downloaded vendors:
Then I added a new
test.phpfile with this content:And when I ran
php -f test.phpthe email was sent as expected. I got it with the same copy and content I set so the whole workflow must be fine. This was delivered on my INBOX:Unless we get a full stack trace where we can see that Mailgun SDK is related then I must assume the issue is not related with Mailgun.
If anyone comes here and had the same issue, I managed to solve it by disabling profiling in the httplugbundle configuration which prevents the ProfileClient to be used and thus prevents the CurlCommandFormatter to be called.
@michaeldk , I’ve got the same problem. Disabling profiling really helped. Thanks for saving a lot of my time.
@michaeldk Thank you for your insights! I wondered how the
CurlCommandFormattergot pulled into this. I reported the issue in php-http/message#91.Hey @DavidGarciaCat 😃
Did you have any time to run some tests?
Thanks!