News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Sending a fax does not work when you have attachments
Tags: fax
Feb 8, 2022 at 11:28am   •   2 replies  •  0 likes
Ivan Irias

I have the following problem:

I am using the code for sending a fax as it is published in the official documentation.


// Send Fax

$request = $rcsdk->createMultipartBuilder()
                 ->setBody(array(
                     'to'         => array(
                         array('phoneNumber' => xxxxxxxxxxxxx),
                     ),
                     'faxResolution' => 'High',
                 ))
                 ->add('Plain Text', 'file.txt')
                 ->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r'))
                 ->request('/account/~/extension/~/fax');

//print $request->getBody() . PHP_EOL;

$response = $platform->sendRequest($request);

print 'Sent Fax ' . $response->json()->uri . PHP_EOL;


This works perfectly for me in my local development environment. (Windows 10, WampServer, php 7.2.25, Laravel 5.5.9). But when I do the deploy in LIVE (aws, ngix, php 7.2.25, Laravel 5.5.9) it doesn't send the fax and it gives me the following error:

ERROR: 400 Bad Request (and additional error happened during JSON parse: Response is not JSON) {"userId":00,"email":"xxxxxx@xxx.com","exception":"[object] (RingCentral\\SDK\\Http\\ApiException(code: 400): 400 Bad Request (and additional error happened during JSON parse: Response is not JSON) at /home/forge/onemed.us/vendor/ringcentral/ringcentral-php/src/Http/Client.php:52, Exception(code: 0): Response has unsuccessful status at /vendor/ringcentral/ringcentral-php/src/Http/Client.php:44)
[stacktrace]
#0 vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(310): RingCentral\\SDK\\Http\\Client->send(Object(GuzzleHttp\\Psr7\\Request))
#1 app/Http/Controllers/RingCentralController.php(353): RingCentral\\SDK\\Platform\\Platform->sendRequest(Object(GuzzleHttp\\Psr7\\Request))


This occurs when there are files attached to the fax. When there is none, the fax is sent

I have read in other threads that there are users with some similar problems but they do not detail the solution.



2 Answers
answered on Feb 10, 2022 at 12:07am  

@Ivan Irias
What you seem to be missing is adding a form boundary to be used in the multipart form request
The SDK will take care of creating the form data for you with multiple attachments but you need to provide a form data boundary


<?php
require('vendor/autoload.php');


$RECIPIENT = '<NUMBER>';
$RINGCENTRAL_CLIENTID = '<CLIENT_ID>';
$RINGCENTRAL_CLIENTSECRET = '<CLIENT_SECRET>';
$RINGCENTRAL_SERVER = RingCentral\SDK\SDK::SERVER_PRODUCTION;


$RINGCENTRAL_USERNAME = '<USERNAME>';
$RINGCENTRAL_PASSWORD = '<PASSWORD>';
$RINGCENTRAL_EXTENSION = '';


$rcsdk = new RingCentral\SDK\SDK($RINGCENTRAL_CLIENTID, $RINGCENTRAL_CLIENTSECRET, $RINGCENTRAL_SERVER);


$platform = $rcsdk->platform();
$platform->login($RINGCENTRAL_USERNAME, $RINGCENTRAL_EXTENSION, $RINGCENTRAL_PASSWORD);


$request = $rcsdk->createMultipartBuilder()
->setBody(array(
  'to' => array(array('phoneNumber' => $RECIPIENT)),
  'faxResolution' => 'High',
  'coverPageText' => 'Sending using PHP'
))
->setBoundary('--multipart-form-boundary--')
->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r'))
->add('First page with text', 'first.txt')
->add('Second page with text', 'second.txt')
->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r'))
->request('/account/~/extension/~/fax');


$resp = $platform->sendRequest($request);
print_r ("FAX sent. Message status: " . $resp->json()->messageStatus);
?>


This should be helpful

Let me know if you are still stuck.

Best,
Yatin


 0
answered on Feb 8, 2022 at 2:23pm  

It could be that the headers Content-Type for Send Fax might be wrong . The content-type is supposed to be 'multipart/mixed'

Can you change your code to something like this ?


@Yatin Gera @Yatin Gera Can you please test the PHP SDK and make changes as necessary ?

 $builder = new MultipartBuilder();
 $builder->setBody(['to' => ['phoneNumber' => 'foo'], 'faxResolution' => 'High'])
                ->setBoundary('boundary')
                ->add('plain text', 'plain.txt', ['Content-Type' => 'text/custom']);

 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us