Send SMS via Python, Shell with Clickatell, Gmail
I wanted to be able to send a text message via the command line, so I wrote a little Python program to do it. It uses Clickatell's HTTP API, which you have to pay for (it's $.05 a message). I thought I'd share, since the other ones written in Python are too complex. You can run it at the shell,
#sms.py "Test message to default phone number"
#sms.py "Test message to the following number" 13605551234
or import it into other Python programs.
#!/usr/bin/env python
import urllib
import sys
#A non-overly complicated python interface to Clickatells.com's HTTP api.
#Import into python, or use as shell script. Be sure to edit the default
#parameters in the send function below
def send(text, to = "13605555555", send_as = "13605555556", user = "your_username", password = "your_password", api_id = "your api key"):
url = "http://api.clickatell.com/http/sendmsg"
config = {}
config['user'] = user
config['password'] = password
config['api_id'] = api_id
config['from'] = send_as
config['to'] = to
config['text'] = text
query = urllib.urlencode(config)
file = urllib.urlopen(url, query)
output = file.read()
file.close()
return output
if __name__ == '__main__':
argc = len(sys.argv)
# first argument is the text message
if argc == 2:
print send(sys.argv[1])
# arguments are a text message and a phone number
elif argc == 3:
print send(sys.argv[1], sys.argv[2])
else:
print 'Usage: sms.py "Text message" [phone number]'
I didn't use PHP because I didn't want to install it on the server, and I wanted to practice my Python
decibel.places posted this at 01:38 — 3rd May 2009.
He has: 1,494 posts
Joined: Jun 2008
cool, this should be a portfolio project
pr0gr4mm3r posted this at 01:42 — 3rd May 2009.
He has: 1,502 posts
Joined: Sep 2006
Cool, I will have to look into that API. The only thing is that I don't like the services that charge for SMS messages. I just usually use the email address that it tied to the number. Most if not all providers have [email protected] as an option, which is what I use when I set up alert systems for my own use.
teammatt3 posted this at 02:06 — 3rd May 2009.
He has: 2,102 posts
Joined: Sep 2003
That's a good idea. I thought I would have to install a mail server on the machine to do that. But I just realized you could create a simple HTTP request to a mailing script on another machine that has mailing services. Now I spent $20 for nothing
I think it's a total ripoff to pay between $.05 and $.20 for a few bytes of data transfer too. I can't wait for the day when SMS is free.
teammatt3 posted this at 02:30 — 3rd May 2009.
He has: 2,102 posts
Joined: Sep 2003
Even better, use Gmail as your mail server, and use your provider's free gateway:
#!/usr/bin/env python
import sys
import smtplib
def send(body, to = "[email protected]", username = "[email protected]", password = "my password"):
mail_server = smtplib.SMTP("smtp.gmail.com", 587)
mail_server.ehlo()
mail_server.starttls()
mail_server.ehlo()
mail_server.login(username, password)
mail_server.sendmail(username, to, body)
mail_server.close()
if __name__ == '__main__':
argc = len(sys.argv)
# first argument is the text message
if argc == 2:
send(sys.argv[1])
# arguments are a text message and an email
elif argc == 3:
send(sys.argv[1], sys.argv[2])
else:
print 'Usage: gmail.py "Text message" "[phone number as email]"'
pr0gr4mm3r posted this at 11:40 — 3rd May 2009.
He has: 1,502 posts
Joined: Sep 2006
The only problem with finding the email address is that you can't really tell what it is just by looking at the phone number. You need the site users to select their cell provider as well, and then have a data set that can match the email domain with the provider list.
decibel.places posted this at 15:53 — 3rd May 2009.
He has: 1,494 posts
Joined: Jun 2008
excuse a question from a Python n00b (yes, believe it or not, I don't know everything )
so if I put this code in a file named sms.py on my server I can then send a SMS from PuTTY using SSH?
where should I put sms.py ?
teammatt3 posted this at 18:08 — 3rd May 2009.
He has: 2,102 posts
Joined: Sep 2003
I wish it was that easy.
If you are using the Clickatell script:
def send(text, to = "13605555555", send_as = "13605555556", user = "your_username", password = "your_password", api_id = "your api key"):
If you are using the Gmail script:
def send(body, to = "[email protected]", username = "[email protected]", password = "my password"):
And of course, it only works if you have Python (Debian has it by default, other distros probably do too)
You can put it anywhere on your server. In my home directory, I created a directory called "bin" that is in my $PATH variable. I can call gmail.py anywhere on the command line. If you don't put the script in your $PATH, then you always have to call the script with "./gmail.py" or "/path/to/gmail.py"
decibel.places posted this at 19:59 — 3rd May 2009.
He has: 1,494 posts
Joined: Jun 2008
haha - I don't even own a cell phone
thinking about getting the G1, sometime
people ask me, "How do you live without a cell phone?"
I answer, "In peace."
hhunt posted this at 09:50 — 31st March 2010.
They have: 17 posts
Joined: Oct 2009
Thanks guys, this looks very cool. I will try and run it when I get home to see if works for me as well.
Nice for posting it.
adforte posted this at 21:55 — 27th August 2010.
They have: 1 posts
Joined: Aug 2010
Send sms via HTTP request, sample url: http://adforte.com/jovi.member.app-user-commands?action=send&user=yourus...
SMS Gateway URL HTTP SMPP http://www.adforte.com
hhunt posted this at 17:26 — 8th October 2012.
They have: 17 posts
Joined: Oct 2009
I just found myself coming back to this page after a couple of years away. Nice piece of code and this it's still valid today.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.