Python Fax – Send a Binary File Using SendFax
This sample demonstrates how to submit a binary file to the fax Web service. This is done using SendFax with Python.
The SendFax method also indicates whether the submission succeeded. The return value of a successful submission is the transaction ID (positive number) of the fax in the system. Other return values indicate a failure (for more details, see Web Service Return Codes).
As explained above, the following Python fax snippet sends a binary fax from Python and indicates whether the submission succeeded:
"""
A simple script to send an ASCII fax the InterFAX
SendFax SOAP API call.
"""
#from interfax import client
import osa
import base64
from os.path import basename, splitext
# Helper function to convert a file to
def load_file_base_64(file_name):
with open(file_name,"rb") as f:
base64data = base64.b64encode(f.read())
try:
return base64data # python 3
except:
return base64data # python 2
# Create a SoapClient with a link to the WSDL definition.
client = osa.Client("https://ws.interfax.net/dfs.asmx?WSDL")
# Load the file to be sent via fax
file_name = "test1.pdf"
file_data = load_file_base_64(file_name)
# Place file extension in the file_type
file_type = splitext(file_name)[1].strip(".").upper()
print('Testing Sendfax...')
try:
result = client.service.Sendfax(Username='', Password='', FaxNumber='',
FileData= file_data,
FileType= file_type)
print(' Fax was sent with result code: {0}'.format(result))
except Exception as err:
print(err)
"""
In case of successful submission, result will contain a positive
Transaction ID.
A negative value means an error occurred.
See the InterFax Status Codes: https://www.interfax.net/en/dev/webservice/reference/web-service-return-codes
"""