Send SMS with SMPP using Python

Sending SMS with the SMPP API using the Python programming language

The python-smpplib library can be used to make SMPP requests using Python. This library can be used to submit SMS to an SMS gateway or SMSC.

SMPP requirements and resources

SMPP is the Short Message Peer-to-Peer protocol and is used by applications for sending and receiving SMS. An SMPP client can be used to connect to an SMSC or SMS gateway using the SMPP protocol. An SMPP account, including special Developer accounts, can quickly and easily be obtained for using the Melrose Labs Tyr SMS Gateway or SMSC Simulator. The following are required to send SMS with SMPP:

SMPP Protocol [reference]
Short Message Peer-to-Peer Protocol v3.3, v3.4 and v5 specifications and guides

SMPP Client [tool]
Browser-based SMPP client supporting SMPP v3.x and v5 via Web Sockets

Python requirements and resources

Python is a programming language and can be used to quickly and easily add SMS support for programmatically sending and receiving SMS messages. Use it for transactional messaging and notifications between your application and mobiles. The following are required to send SMS using Python:

Python
Python programming language

Requirements and resources

The following are required to send SMS with SMPP using Python:

python-smpplib
Python SMPPlib: SMPP library for Python.

Install

$ git clone https://github.com/python-smpplib/python-smpplib.git
$ cd python-smpplib
$ virtualenv --no-site-packages ve/
$ source ve/bin/activate
(ve)$ pip install six

Code

Create the file sendsms.py containing the code below. Replace the hostname smscsim.melroselabs.com, port 2775, SYSTEMID and PASSWORD values with those from your SMPP API account. Alternatively, replace SYSTEMID and PASSWORD with those allocated to you for use with the Melrose Labs SMSC Simulator.

The following example Python code opens an SMPP transceiver bind to smscsim.melroselabs.com on port 2775 (SMPP port), and then sends the message Hello World #$£ to mobile number 447712345678 from MelroseLabs. The SMPP system ID and password for the SMPP account are contained in SYSTEMID and PASSWORD respectively.

import logging
import sys

import smpplib.gsm
import smpplib.client
import smpplib.consts

# if you want to know what's happening
logging.basicConfig(level='DEBUG')

# Two parts, GSM default / UCS2, SMS with UDH
parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(u'Hello World €$£')

client = smpplib.client.Client('smscsim.melroselabs.com', 2775)

# Print when obtain message_id
client.set_message_sent_handler(
    lambda pdu: sys.stdout.write('sent {} {}\n'.format(pdu.sequence, pdu.message_id)))

# Handle delivery receipts (and any MO SMS)
def handle_deliver_sm(pdu):
        sys.stdout.write('delivered {}\n'.format(pdu.receipted_message_id))
        return 0 # cmd status for deliver_sm_resp

client.set_message_received_handler(lambda pdu: handle_deliver_sm(pdu))

client.connect()
client.bind_transceiver(system_id='SYSTEMID', password='PASSWORD')

for part in parts:
    pdu = client.send_message(
        source_addr_ton=smpplib.consts.SMPP_TON_ALNUM,
        source_addr_npi=smpplib.consts.SMPP_NPI_UNK,
        # Make sure it is a byte string, not unicode:
        source_addr='MelroseLabs',

        dest_addr_ton=smpplib.consts.SMPP_TON_INTL,
        dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN,
        # Make sure these two params are byte strings, not unicode:
        destination_addr='447712345678',
        short_message=part,

        data_coding=encoding_flag,
        esm_class=msg_type_flag,
        registered_delivery=True,
    )
    print(pdu.sequence)

# Enters a loop, waiting for incoming PDUs
client.listen()

The above shows a connection being made to smscsim.melroselabs.com on port 2775 and an SMPP transceiver bind (client.bind_transceiver()) being established. Once this has been done, a message is submitted to the SMSC/SMS gateway using client.send_message() (submit_sm PDU), and a response (submit_sm_resp PDU) is received from the SMSC/SMS gateway with the message ID for the submitted message. Shortly afterwards, the message is delivered and a delivery receipt contained in a deliver_sm PDU is received, to which our code responds with an acknowledgement (deliver_sm_resp PDU).

Using in production

Whatever the language or API, you can send SMS and receive SMS between applications and mobiles for a wide range of uses with any of the trusted and reliable CPaaS services from Melrose Labs. Take a look at our Messaging, SMS gateway and Bulk SMS solutions, and sign-up for a Developer account on our Tyr SMS Gateway service to try us out.

We provide a wide range of CPaaS services and infrastructure to organisations, including cloud platforms that enable you to run your own SMS gateway.

Get in contact with us to find out more about CPaaS voice, messaging, video and identity from Melrose Labs.

Testing

SMSC Simulator

For testing your application's SMS support when using the SMPP protocol, we recommend starting with the Melrose Labs SMSC Simulator service to simulate SMS message delivery to mobiles (MT SMS) and simulate SMS messages from mobiles (MO SMS). The SMSC Simulator supports SMPP v3.3, v3.4 and v5.

Tyr SMS Gateway

For live testing and delivery to mobiles, use the reliable and dependable Melrose Labs Tyr SMS Gateway for A2P, P2A, bulk, wholesale and business SMS, text marketing and other uses. The Melrose Labs Tyr SMS Gateway supports REST and SMPP APIs.

Alternative APIs and languages

Other APIs covered in our tutorials that can be used for sending and receiving SMS using Python include: Send SMS with REST using Python

Other languages covered in our tutorials that can be used for sending and receiving SMS with SMPP include: PHP, Java, C++, C#, Perl, Go, Node.js, Ruby