Friday, April 15, 2016

Sending SMS/Text Message with Python Script

How to Send an SMS/Text Message with Python

Text messages are more immediate and reliable way of sending notifications than email as most people are more likely to be near their phones than their computers.

Some Python SMS gateway providers are listed below:-

In this tutorial, you’ll learn how to configure an SMS gateway service and use its Python module to send text messages. You will be creating an SMS/Text Messaging Script with Python.

There are several SMS gateway services to choose from as listed above; however I am going to use SMS gateway service called “Twilio”.

Twilio is an SMS gateway service, which means it’s a service that allows you to send text messages from your programs (this program can be a desktop, mobile or web application). I will use the FREE trial service for this demonstration, feel free to upgrade if need enhanced services.

Twilio SMS texting services are available in several countries, but the specifics in this tutorial are for Nigeria.

Lets get started...

Install Twilio Python Module

Before signing up for a Twilio account, install the twilio module by running:

pip install twilio

If your Windows-PC is connected to internet, the twilio module will be downloaded and installed on your default python installation.

Signing Up for a Twilio Account

Go to and fill out the sign-up form. Once you’ve signed up for a new account, you’ll need to verify a mobile phone number that you want to send texts to. (This verification is necessary to prevent people from using the service to spam random phone numbers with text messages.)

After receiving the text with the verification number, enter it into the Twilio website to prove that you own the mobile phone you are verifying. You will now be able to send texts to this phone number using the twilio module.

Since we plan to use twilio for messaging, please click the Get your "Twilio Number" button after verification. Note that this number will be disabled after trial period expires.

Twilio provides your trial account with a phone number to use as the sender of text messages.
You will need two more pieces of information: your account SID and the auth (authentication) token.

You can find this information on the Dashboard page when you are logged in to your Twilio account under “Show API Credentials”. These values act as your Twilio username and password when logging in from a Python program.

To send SMS to any part of the world, you have to enable the location in your account. To do this, go to your Twilio account SMS International Permissions page to enable this location below:
In my own case I enabled all the countries including Nigeria.

The code

If you have followed above process correctly, then this simple line of code below will send the body variable text as SMS to the recipient (to variable) phone number from your twilio phone number in the from_ variable;-

from import TwilioRestClient

# Find these values at
account_sid = "BW39526axxxxxxxxxx6535bf1721j9"
auth_token = "6Yb3xxxxxx3c27f7b18a77c5bf4uy6f"
client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(to="+2348039508010",
        body="Umar Yusuf - This is an SMS API with Python")

The code above will send the this text "Umar Yusuf - This is an SMS API with Python" to my number as seen below. Make sure you replace the specific details in the code with you account details for you code to work.

Note that you can use any text example "Hello world!" in the body variable. But since my account is atrial account, the SMS will deliver with prefix text as seen above.

Now that we know how to send SMS with python script, lets embed our SMS script into a function foe;-
1- A Desktop Application
2- A Website Application
3- A Mobile Application

Thanks for reading.


  1. Notice to some elements spamming this post, I will report your spam URLs and will never publish it here.

    Find a litigate way to advertise your text/SMS service not spamming blogs.


  2. Hello pls which country number do u used to send sms to a Nigerian number?because Icouldn't see a Nigerian number from the list.