Python Fax – Send Multiple Files to Multiple Recipients

 Download the Python samples fax API zip file.
 Download the Modified OSA files.

This Python fax sample uses the SendfaxEx_2 method to send a fax from Python to one or more recipients with one or more attached files. SendfaxEx_2 has more flexible sending capabilities than SendFax (for more details, see Side-by-side Comparison of Web Service Methods).

In the Python fax snippet below, a few files are read and submitted to the fax Web service, and the return value 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).


"""
A simple script to send an ASCII fax the InterFAX  
SendFaxEx_2 SOAP API call.
"""

import osa
import base64
import datetime
import os



# Helper function to convert a file to
def load_file_bytes(file_name):
    with open(file_name, "rb") as f:
        file_bytes = f.read()
        print(file_name, "File len: ",len(file_bytes))
        file_size =  f.tell()
        return file_bytes, file_size  # return contents and file size


print('Testing SendfaxEx_2...')
all_file_bytes = bytes()
all_file_data = ""
all_file_sizes = ""
all_file_ext = ""

#for file_name in ["test1.pdf"]:
for file_name in ["test1.pdf","test2.pdf"]:
    file_bytes, file_size = load_file_bytes(file_name)
    file_ext = os.path.splitext(file_name)[1]  # get the file extension

    all_file_bytes += file_bytes  #encode_file_contents(file_bytes) # Collect all file contents together into one byte string

    # Create the file type and size data to be sent.
    all_file_ext += ";" + file_ext.strip(".").upper()
    all_file_sizes += "{};".format(file_size)

all_file_sizes = all_file_sizes.strip(";")
all_file_ext = all_file_ext.strip(";")
all_file_data = all_file_bytes

postpone_time = datetime.datetime(2016, 3, 1)
print(all_file_sizes)
print(all_file_ext)
print(len(all_file_data))
try:
    client = osa.Client("https://ws.interfax.net/dfs.asmx?WSDL")
    result = client.service.SendfaxEx_2(Username='',
                                        Password='',
                                        FaxNumbers=';',
                                        Contacts='',
                                        FilesData=all_file_data,
                                        FileTypes=all_file_ext,
                                        FileSizes=all_file_sizes,
                                        Postpone=postpone_time,
                                        RetriesToPerform=1,
                                        CSID="My CSID",
                                        PageHeader ="To: {To} From: {From} Pages: {TotalPages}",
                                        JobID = "",
                                        Subject='Test Subject',
                                        ReplyAddress = "",
                                        PageSize="A4",
                                        PageOrientation ="",
                                        IsHighResolution=True,
                                        IsFineRendering=False)



    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
"""