How To: Send SMS Messages with Python

Send SMS Messages with Python

In this article, I'll show you how to send SMS messages with Python. You'll need Python 2.7 or later, urllib and urllib2. The code basically uses an online text messaging service to "POST" html data, as if a person was entering the data themselves. The uses for something like this are unlimited. For example, I modified the basic code so I would receive a text message letting me know every time someone rang my doorbell. The program could interface with Arduino through a serial port, and send data to Python, which in turn forwards it to your phone. Or you could even use the program to notify you when you receive an email. Check out this article on how to make a python Gmail notifier. 

The Code

The first part of the code (provider check) takes the first six digits of the entered phone number and checks for the provider, then associates it with the correct "POST" command. The second part (text sender) takes the full phone number and message, and sends it to the online texting site. 

import urllib
import urllib2
number = raw_input("Please Enter The Phone Number:\n")
message = raw_input("Please Enter Your Message:\n")
prov = ''
url2 = 'http://www.txt2day.com/lookup.php'
url = 'http://www.onlinetextmessage.com/send.php'
values2 = {'action' : 'lookup',
           'pre' : number[0:3],
           'ex' : number[3:6],
           'myButton' : 'Find Provider'}
data2 = urllib.urlencode(values2)  ##provider checker
req2 = urllib2.Request(url2, data2)
response2 = urllib2.urlopen(req2)
the_page2 = response2.read()
if 'Telus' in the_page2:
    prov = '192'
if 'Bell' in the_page2:
    prov = '48'
if 'Rogers' in the_page2:
    prov = '162'
if 'Sprint' in the_page2:
    prov = '175'
if 'T-Mobile' in the_page2:
    prov = '182'
if 'Verizon' in the_page2:
    prov = '203'
if 'Virgin Mobile' in the_page2:
    prov = '205'
if 'AT&T' in the_page2:
    prov = '41'
print prov
if prov == 0:
    print "Failed To Identify Provider\n"
    exit
values = {'code' : '',
          'number' : number,
          'from' : '',
          'remember' : 'n',
          'subject' : '',
          'carrier' : prov,
          'quicktext' : '',
          'message' : message,
          's' : 'Send Message'}
data = urllib.urlencode(values)  ##text sender
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read() 

Possible Mods

If the code fails to find your provider, enter the number here, and look for the associated number code to your cell phone provider here. Insert an "if 'YourCellPhoneProvider' in the_page2: prov = 'AssociatedNumber'". If you'd like to use another online text messaging service, use Firebug to identify the form parameters, and change the "values" variables to the POST values. Enjoy! :) 

4 Comments

Looks like you figured it out on your own ;). Nice job!

awesome script. Is it still functional with the current websites?

Can you achieve the same result (phone carrier reverse lookup) in php? and How? Any help? thanks

hi friends,

i am executing this program by connecting my Arduino yun board through Ethenet. i am using ssh command to get into the IP address of my Ethernet. then i am running this program, i am not having any errors but i cant receive any sms to my cell phone. what is the problem? please help me out to solve this problem.

thanks

Share Your Thoughts

  • Hot
  • Latest