Here comes another tutorial about Send SMS using Custom Gateway with SMPP and Python3, which I had developed at my office. This is one of the tutorials which was long due for publishing.
At my work, one of my responsibility was the SMSC system. We had to provide a gateway to clients. I had to deal with the various client using a different system and most of the time we used to provide SMPP connection.
So to avoid huge queries, I developed simple application in Python 3 using smpplib and tkinter module. With use of Tkinter, I could provide a simple GUI.
Apart from the above use case, I even made a fun application that use to get quotes from the internet and send during tea time. I used that application among my colleagues, it was a fun experience. For getting the codes in this case, I used Web Scrapping as explained in my other tutorial titled Web Scraping Using Python 3 and Beautiful Soup.
Exploring online tutorial, I found most people are using Twilio or other paid gateway. So here is one which uses custom gateway.
Getting Started
You have seen some use cases. What will you be using this application for? Let me know in the comment section.
To get started we need following things installed in your computer.
- Python 3
- smpplib
- Tkinter
We will be using Python 3 which you can download from the Official Python website. For creating GUI you will need Tkinter. As the Tkinter will come along with the Python 3 you are covered. Only the smpplib is the one you need to install. You can install by typing following in terminal or CMD:
# For Windows user pip install smpplib # For Mac/Linux user pip3 install smpplib
Installation might take few seconds to minute depending on the network speed.
Now you are all set. Create an empty python file. We will first code and run on the terminal, after that we will create simple GUI.
Creating SMPP Application on Terminal
Here we will include the connection details in the file itself and run it. To do that, we will start by importing the required modules from smpplib like smpplib.client, smpplib.consts and smpplib.gsm.
import smpplib.client, smpplib.consts, smpplib.gsm
Next we will have different variables holding different information for the credential
SMSC_IP = "xxx.xxx.xxx.xxx"
SMSC_PORT = xxxx
SYSTEM_ID = "username"
SYSTEM_PASS = "password"
SENDER_ID = "InstaCodeBlog"
RECEIVER_NO = xxxxxxxxxx
MESSAGE = "Test Message from InstaCodeBlog"
Next we will connect to the SMSC gateway and then bind to gateway with the credentials.
client = smpplib.client.Client(SMSC_IP, SMSC_PORT)
client.connect()
I want to mention that while binding, there are basically two types namely Transmitter and Transceiver. So you need to take care of that.
# Transmitter
client.bind_transmitter(system_id=SYSTEM_ID, password=SYSTEM_PASS)
# Transceiver
client.bind_transceiver(system_id=SYSTEM_ID, password=SYSTEM_PASS)
Next you will follow with sending of the message. As message will be converted into parts, so we will use for loop.
for part in parts:
client.send_message(
source_addr_ton=smpplib.consts.SMPP_TON_INTL,
source_addr = SENDER_ID,
dest_addr_ton=smpplib.consts.SMPP_TON_INTL,
destination_addr = RECEIVER_NO,
short_message = part,
data_coding = encoding_flag,
esm_class = msg_type_flag,
registered_delivery = True,
)
That is all. Your final code should look something like this:
# import required modules
# Package for Sending SMS using SMPP
import smpplib.client, smpplib.consts, smpplib.gsm
# SMSC Connection Details
SMSC_IP = "xxx.xxx.xxx.xxx"
SMSC_PORT = xxxx
SYSTEM_ID = "username"
SYSTEM_PASS = "password"
SENDER_ID = "InstaCodeBlog"
RECEIVER_NO = xxxxxxxxxx
MESSAGE = "Test Message from InstaCodeBlog"
# Converting message into parts
parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(MESSAGE)
# Connect to the remote SMSC gateway
client = smpplib.client.Client(SMSC_IP, SMSC_PORT)
client.connect()
# Bind to the SMSC with the credentail
client.bind_transceiver(system_id=SYSTEM_ID, password=SYSTEM_PASS)
# Getting the message from parts and sending it
for part in parts:
client.send_message(
source_addr_ton=smpplib.consts.SMPP_TON_INTL,
source_addr = SENDER_ID,
dest_addr_ton=smpplib.consts.SMPP_TON_INTL,
destination_addr = RECEIVER_NO,
short_message = part,
data_coding = encoding_flag,
esm_class = msg_type_flag,
registered_delivery = True,
)
client.unbind()
client.disconnect()
You are done for simple SMS sending client developed in Python 3 for SMPP protocol.
Next we will proceed with development with simple GUI.
Creating GUI for the SMPP Application
You have seen how to develop SMS sending program using Python 3 above. If you are a developer or technical person working on a server the terminal version would be better. Else if you are like me working with clients who are from a non-technical background, having a GUI version is better.
So let us get into the code.
For GUI version too the SMS sending technique remains the same as mentioned above. Except here we will use additional Tkinter module of Python.
Also, we will use a module called logging, this will be used to generate a log file. As mentioned above if your client is from a non-technical background and says that the GUI is not working. You can ask this log, so you can debug the issue further. We will save the log as sms_smpp.log file.
I had already uploaded the code in a GitHub repository. You can find the link below:
If you want to learn how to upload your project to GitHub, you can read my tutorial on UPLOAD YOUR PROJECT TO GITHUB.
When you run the code your GUI should open as shown in the image below. After then fill the details and press that submit button.
In that repository, I have uploaded the executable for Windows, Mac and Linux. You can download and execute those file. Having the executable, you will not be required to install Python or any of the module. It would be the perfect piece to share with your clients.
Conclusion
That is all folks!!
With this tutorial, you have not only learned how to code and develop an application to send SMS for SMPP protocol using Python 3 but also develop GUI using Tkinter.
If you have any queries or issues faced, mention it in the comment section. Also, if you have found a better way to develop the application, please mention it. It will help other readers and I can learn too.