Conference Campaign Solo Rich Messaging

Tutorial section - SMS

Send SMS with SMPP using Python

Sending SMS with the SMPP API using the Python programming language

The php-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 for conveying SMS operations. An SMPP account can easily be obtained for using the Tyr SMS Gateway or SMSC Simulator. The following are required to send SMS with SMPP:

  • SMPP API account - SMPP API account for the Melrose Labs Tyr SMS Gateway
  • SMPP API account (simulator) - SMPP API account for the Melrose Labs SMSC Simulator. Allows you to simulate inbound and outbound SMS delivery using an SMPP API account with your application.
  • SMPP Protocol [reference] - Short Message Peer-to-Peer Protocol v3.3, v3.4 and v5 specifications and guides

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:

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 thease 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()

Run sendsms.py:

	(ve)$ python sendsms.py 
INFO:smpp.Client.4495364560:Connecting to smscsim.melroselabs.com:2775...
DEBUG:smpp.Client.4495364560:Receiver mode
DEBUG:smpp.Client.4495364560:Sending bind_transceiver PDU
DEBUG:smpp.Client.4495364560:>>b'000000250000000900000000000000013731323736320050415353574f5244000034000000' (37 bytes)
DEBUG:smpp.Client.4495364560:Waiting for PDU...
DEBUG:smpp.Client.4495364560:<<b'000000208000000900000000000000014d656c726f73654c616273534d534300' (32 bytes)
DEBUG:smpp.Client.4495364560:Read bind_transceiver_resp PDU
DEBUG:smpp.Client.4495364560:Sending submit_sm PDU
DEBUG:smpp.Client.4495364560:>>b'000000480000000400000000000000020005004d656c726f73654c616273000101343437373132333435363738000000000000010000001048656c6c6f20576f726c64201b650201' (72 bytes)
2
DEBUG:smpp.Client.4495364560:Waiting for PDU...
DEBUG:smpp.Client.4495364560:<<b'000000518000000400000000000000023039353732613061303962633733663265393065393338626336656138636132646366306364343562343039383165343632396638343035353534376561333100' (81 bytes)
DEBUG:smpp.Client.4495364560:Read submit_sm_resp PDU
sent 2 b'09572a0a09bc73f2e90e938bc6ea8ca2dcf0cd45b40981e4629f84055547ea31'
DEBUG:smpp.Client.4495364560:Waiting for PDU...
DEBUG:smpp.Client.4495364560:<<b'000001260000000500000000000000010001013434373731323334353637380005004d656c726f73654c616273000400000000000000009d69643a30393537326130613039626337336632653930653933386263366561386361326463663063643435623430393831653436323966383430353535343765613331207375623a30303020646c7672643a303031207375626d697420646174653a3230303231383132313020646f6e6520646174653a3230303231383132313020737461743a44454c49565244206572723a30303020746578743a00042700010204230003030000001e00413039353732613061303962633733663265393065393338626336656138636132646366306364343562343039383165343632396638343035353534376561333100' (294 bytes)
DEBUG:smpp.Client.4495364560:Read deliver_sm PDU
delivered b'09572a0a09bc73f2e90e938bc6ea8ca2dcf0cd45b40981e4629f84055547ea31'
DEBUG:smpp.Client.4495364560:Sending deliver_sm_resp PDU
DEBUG:smpp.Client.4495364560:>>b'0000001180000005000000000000000100' (17 bytes)
DEBUG:smpp.Client.4495364560:Waiting for PDU...


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).

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


Whatever the language or API, you can send SMS and receive SMS between applications and mobiles for a wide range of uses with a number of our services. Please consider using our Messaging, SMS gateway and Bulk SMS solutions. If you are interested in testing your application's SMS support and are using the SMPP protocol, then we recommend that you take a look at the Melrose Labs SMSC Simulator service that can simulate SMS message delivery to and from mobiles. The SMSC Simulator supports SMPP v3.3, v3.4 and v5, and has a high degree of SMPP protocol conformance. Later in 2020 we will be providing access to the Melrose Labs Tyr SMS Gateway for A2P, P2A, bulk, wholesale and business SMS, and for text marketing and other applications. The Melrose Labs Tyr SMS Gateway will support REST and SMPP APIs.

Follow us on LinkedIn for updates on Melrose Labs services and news. Follow Us