Wednesday, September 16, 2015

So after a few years break, I have decided to update my blog with some useful articles. After visiting ciscolive 2015 in San Diego, I have began learning Python. So next few blog entries will be code examples, how python help me to get rid of annoying operational stuff. I am not a professional developer, so my codes definitely not written in a best way. So, programmers do not judge me.

In this article I will try to explain how to send SMS via Python using SMPP protocol.
Below you can see the code part that I use:

import smpplib

mobilephone = '12024561111'
msg = 'Hi, Mr. President'
 def SENDSMS(mobilephone, msg):
       client = smpplib.client.Client('10.10.10.1', '2775')
       client.connect()
       client.bind_transceiver(system_id='username',password='password')

       pud = client.send_message(
             source_addr_ton = 5,
             source_addr_npi = 0,
             source_addr = 'Putin',
             dest_addr_ton = 1,
             dest_addr_npi = 1,
             destination_addr = mobilephone,
             short_message =msg)
       client.disconnect()

SENDSMS(mobilephone, msg)

Firstly you need to install smpplib. Try to install via 'pip install smpplib' . It worked for me.
Then I have assigned receiver's phone number to (mobilephone) variable and message to (msg) variable. Afterwards I have defined function call SENDSMS. Function takes to parameters - mobilephone and msg. Within function you can see that I have assigned ip address and smpp port number, username and password that I have got from SMPP provider. Under the client.send_message I have indicated sms parameters. You can get source_addr/destination_addr ton and npi values from your smpp provider. For more information about ton and npi please refer to: http://www.nowsms.com/ton-and-npi-settings-for-smpp-and-ucpemi.
Finally I have passed mobilephone and msg values to function.
Keep in mind that copy pasting of above code might not work as there will be indentation errors. Also if you try to send concatenated messages there might be some errors. I have tested above code only to send short messages.
For more information please refer to:
https://github.com/podshumok/python-smpplib

Thanks,
Nuran Afrasiyabov.