Thursday, February 19, 2009

Python Script to Send SMS via Gmail

Recently I wrote this little script to send a text message to my phone using my Gmail account. It uses a python library called libgmail. (Debian / Ubuntu package: python-libgmail should do the trick).

My cell carrier is Alltel -- you'll have to change myCellEmail to match that of your carrier (list provided courtesy of wikipedia).

#!/usr/bin/env python
import libgmail

stuff = libgmail.GmailAccount("me@gmail.com", "password")
myCellEmail = "1234567890@message.alltel.net"

stuff.login()
msg=libgmail.GmailComposedMessage(myCellEmail, "", "Hello World! From python-libgmail!")
stuff.sendMessage(msg)

Cheers!

5 comments:

guptha said...

hi steve ,
Like gmail can we send sms via Yahoo mail service .If yes ,what could be the library that helps to do that .If you post the code it could be more helpful.
Your blog is informative ,Nice work.
Ganesh

S.J. Fuhry said...

hi guptha! thanks for your comment.

this script would be much more portable if it could simply use SMTP, which would work with just about any email service.

However, the reason I used libgmail instead of some SMTP library was because my ISP (like most residential broadband services) does not allow anyone to send email from port 25 UNLESS it is through their SMTP server. ..and I seriously don't even remember what my email and password is for that.

libgmail, however, connects to Gmail via port 80 (i *think* ..it's not 25 at least) so it avoids that whole problem.

I looked into it, and there doesn't appear to be any such library for yahoo as there is for Gmail, so I don't think it is possible in 6 lines of python like this was :)

I think the easiest way for you is going to be to use SMTP from wherever you're at if you don't want to use libgmail. Here's a script I found that looks like it should work for you:
how to send email from python script.

Hope this works, Cheers!

Trigger said...

This script has helped me a lot. I can now include it other scripts to have a txt message sent to me to notify about certain occurances on my server.

Just wanted to say thanks.

gcoder said...

I'm trying to use the code you outlined here, but I get this error:
AttributeError: 'module' object has no attribute 'GmailAccount'

Any ideas? I installed all dependencies and then installed the libgmail module...

linko47 said...

Not working for me.

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=539927

S.J. Fuhry's Favorite Books

  • Aristotle, "Nicomachean Ethics"
  • Augustine, St., "Confessiones"
  • Barron, Fr. Robert, "Heaven in Stone and Glass"
  • Barron, Fr. Robert, "The Strangest Way"
  • Benedict XVI, "Deus Caritas Est"
  • Chesterton, G.K., "Orthodoxy"
  • Chesterton, G.K., "The Ballad of the White Horse"
  • Chesterton, G.K., "The Dumb Ox"
  • Chesterton, G.K., "The Everlasting Man"
  • Chesterton, G.K., "The Well and the Shallows"
  • John Paul II, "Fides et Ratio"
  • John Paul II, "Theology of the Body"
  • John Paul II, "Veritatis Splendor"
  • Leo XIII, Pope, "Rerum Novarum"
  • Lewis, C.S., "The Abolition of Man"
  • O'Connor, Flannery, "A Good Man Is Hard to Find and Other Stories"
  • Pearce, Joseph, "Literary Converts"
  • Pearce, Joseph, "Tolkien: Man and Myth"
  • Pearce, Joseph, "Wisdom and Innocence"
  • Ratzinger, Joseph Cardinal, "The Ratzinger Report"
  • Ratzinger, Joseph Cardinal, "The Spirit of the Liturgy"
  • Shakespeare, "Hamlet"
  • Shakespeare, "Henry V"
  • Shakespeare, "The Tempest"
  • Sokolowski, Robert, "Introduction to Phenomenology"
  • Sokolowski, Robert, "The God of Faith and Reason"
  • Tolstoy, Leo, "The Death of Ivan Ilyich"
  • von Balthasar, Hans Urs, "Prayer"
  • Waugh, Evelyn, "Brideshead Revisited"
  • Wiegel, George, "Letters to a Young Catholic"
  • Wojtyla, Karol (John Paul II), "Love and Responsibility"