question

Manny Baylor avatar image
Manny Baylor asked ·

Sending a fax attachment using PowerShell

Hi, I've been accessing your API via PowerShell and everything has gone pretty smooth up to this point. However, now I'm trying to write a script that is able to send a fax with an attachment and I'm having issues with submitting the fax job if it has an attachment. The only type of attachment I have been able to send up to this point is plain text document, but I'd like to be able to send a PDF.

I'm currently sending using the multipart/mixed format. I tried to mimic the format that is in your API Reference page for createFaxMessage where the content-type is set as "multipart/form-data" and transmit the file in base64 encoded format, but at best I get the base64 data as plain text after the cover page.

Here is a copy of the message body as I'm sending it over. I've also set the content type as application/pdf which does not change the results: https://pastebin.com/phB61jy4

If the Base64 text is very small then the fax will deliver with the second page containing the base64 in plain text format. If the Base64 isn't very small (like the sample pdf in my pastebin which is 26kb), then I won't receive the fax at all. If I run a MessageStore API request a few minutes later, I see the status of the message updated to indicate messageStatus=SendingFailed; faxErrorCode=RenderingFailed

Is there a way to structure the message body so that your API recognizes the file as a PDF and decodes it properly? Since I'm using PowerShell to do this I'm unable to use your pre-packaged API, but up to this point it hasn't been a problem. If only I knew what your traffic API processor was looking for, I could probably shape the traffic so that it would work without issue.

fax apifax attachment
2 comments
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

I don't have any ready code your you. But if you post part of your code where you create and post your request then I can help having a look it it for you.

0 Likes 0 · ·

Phong, please see my pastebin link below. It won't let me post because the code is roughly 1500 characters.

https://pastebin.com/NFcdDtdR

0 Likes 0 · ·
Phong Vu avatar image
Phong Vu answered ·

I found the curl generated by the API explorer (posted in my response above) was incorrect. So basically you don't need to convert to based64 string.

Can you follow the curl example in this blog article to change your script code accordingly.

Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered ·

Thanks for the code reference!

In your question you said setting the content-type as "multipart/form-data" but in the code I found this

"https://platform.devtest.ringcentral.com/restapi/v1.0/account/$($accountId)/extension/$($extensionId)/fax" -Method Post -ContentType "multipart/mixed; boundary=$($boundary)" -Body $RingCentralSendFax -Headers $accesstoken.Header

Not sure what was wrong but can you change the content-type to "multipart/form-data"

Here is a typical curl request to send a fax

curl --request POST \
  --url 'https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/fax' \
  --header 'accept: application/json' \
  --header 'authorization: Bearer xxxxxF8QUE' \
  --header 'content-type: multipart/form-data' \
  --data '{"attachment":"data:application/pdf;name=scan1172.pdf;base64,JVBERi0xLjQ..../......9kKZW5kc3RyZWFVFT0Y=","to":["1650XXXXXXX"],"coverPageText":"Have cover page text"}'


Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Manny Baylor avatar image
Manny Baylor answered ·

Phong, so that's what I originally tried to do when I wrote my script, but no matter what I tried the API processor wouldn't accept the message. I keep getting the "Bad Request" error message.

In the CURL API documentation and in the sample you have provided, it appears that the attachment and the PDF base64 is included in the JSON formatting in the message body. I replicated this in my own message and it doesn't accept the fax even when I don't include an attachment.

Here is the code from that test:
https://pastebin.com/fZTH71H1

This is an exact copy of what the Message Body looks like (although admittedly this particular test:
https://pastebin.com/AiDKngf3

I have not had any issue in sending JSON data with the other messaging API's. Not sure why this is an issue, since this data appears to mimic the CURL formatting in the API Documentation.

3 comments Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

I can't open both links you provided above.

0 Likes 0 · ·

I'm trying to split my source code into 2 separate messages, but for some reason it's not letting me submit Part 1 into the reply box here.

0 Likes 0 · ·

For some reason the submit button worked when I posted as an answer.

I won't be able to post the second pastebin, that one is pretty big.

0 Likes 0 · ·
Manny Baylor avatar image
Manny Baylor answered ·

Part 1

# Pack the JSON DATA

$todestination = [pscustomobject]@()
$todestination += @{
    phoneNumber = "+14435551212"
    name = "Test Recipient"
}

### CONVERT FILE CONTENT TO BASE64 STRING
$FilePath = "$($env:TEMP)\TestFax.PDF"
$objFile = Get-ItemProperty -Path $FilePath
$FileData = [Convert]::ToBase64String([IO.File]::ReadAllBytes($FilePath))

# Create Attachment Param
$attachmentdata = "data:application/pdf;name=$((Get-ItemProperty -Path $FilePath).Name);base64,$($FileData)"

$RingCentralSendFaxJson = [pscustomobject]@{}
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name attachment -Value $attachmentdata
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name to -Value $todestination
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name faxResolution -Value "High"
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name coverPageText -Value "Have cover page text"


1 comment Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Part 2

$RingCentralSendFaxJson = $RingCentralSendFaxJson | ConvertTo-Json

$accountId = "~"
$extensionId = "~"
        
#Send the text message
$sendresponse = Invoke-RestMethod -Uri "https://platform.devtest.ringcentral.com/restapi/v1.0/account/$($accountId)/extension/$($extensionId)/fax" -Method Post -ContentType "multipart/form-data" -Body $RingCentralSendFaxJson -Headers $accesstoken.Header


0 Likes 0 · ·
Manny Baylor avatar image
Manny Baylor answered ·

I was finally able to figure out this issue! Turns out my original code I posted to https://pastebin.com/NFcdDtdR is pretty close, but I needed to make a couple changes in the boundry area where the attachment data is.

1. At line 12 where I previously defined $FileData replace with the following:

# Get File Data
$fileBin = [System.IO.File]::ReadAllBytes($FilePath)
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")

2. Change line 31, name=""fileData"" (though not sure if that matters)

3. Remove line 32

4. Change line 33 to application/pdf

OR you can detect the content type before populating $RingCentralSendFax and reference $ContentType value as the Content-Type

#Determine Mime Type
Add-Type -AssemblyName System.Web
$mimeType = [System.Web.MimeMapping]::GetMimeMapping($FilePath)
if ($mimeType) {
    $ContentType = $mimeType
} else {
    $ContentType = "application/octet-stream"
}

5. Replace line 35 with the following:

$enc.GetString($fileBin),

That should work. Thanks for your help!

Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

alan-kimelman-md avatar image
alan-kimelman-md answered ·

Were your changes successful, and did the API deliver both a rendered pdf document and return a name of the attached document to your account?

I am having trouble returning a meaningful attachment name, as the name of the faxed attachment takes on the temporary folder identification.

Do you know of a method to post a curl so that a new name can replace the file name generated by the temporary folder? This is shown in the example demonstrated at

https://developers.ringcentral.com/api-reference/Fax/createFaxMessage

url --request POST \
  --url https://platform.devtest.ringcentral.com/restapi/v1.0/account/accountId/extension/extensionId/fax \
  --header 'accept: application/json' \
  --header 'content-type: multipart/form-data' \  --data '{"attachment":"data:application/pdf;name=A%Different%Name.pdf;base64,JVBERi0xLjMK…lRU9GCg==","to":["1234567890"]}'

in which I have defined the new name as

  A%Different%Name.pdf

My attempts have failed. Either I generate a rendered pdf doc, or I generate a new name, but not both.

Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.