SMS from Apex to Google Voice number - python script

rxcomm

New member
I thought there might be some interest in a simple python script to parse emails from your aquarium controller and forward on as an SMS message to a Google Voice number. This is useful for those (like me) who don't pay for a texting package on our cell phones, but would still like to get SMS updates to our Google Voice number from our controller.

The script uses the python imaplib package and requires the pygooglevoice package available here. I run the script from crontab on a linux machine, once per minute.

Here is the script:
Code:
#!/usr/bin/python
import imaplib
import email
from googlevoice import Voice
from googlevoice.util import input

def extract_body(payload):
    if isinstance(payload,str):
        return payload
    else:
        return '\n'.join([extract_body(part.get_payload()) for part in payload])

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("userid@gmail.com", "password")
conn.select()
typ, data = conn.search(None, 'UNSEEN')
try:
    for num in data[0].split():
        typ, msg_data = conn.fetch(num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                subject=msg['subject']                   
                #print(subject)
                payload=msg.get_payload()
                body=extract_body(payload)
                #print(body)
                voice = Voice()
                voice.login()
                voice.send_sms(##########, "Subject: "+subject+" Body: "+body)
                typ, response = conn.store(num, '+FLAGS', r'(\Deleted \Seen)')
    typ, conn.expunge()
finally:
    try:
        conn.close()
    except:
        pass
    conn.logout()

To use this script, change userid to your userid, and password to your password for your gmail account in the above (the script should also work with other imap-enabled servers, but I haven't tested it extensively on other than gmail), and change ########## to the phone number you want the SMS messages delivered to. You will also need to configure pygooglevoice per the documentation for that package. Basically, you need to set up a config file called .gvoice in your home directory that looks like the following:
Code:
[auth]
# Google Account email address (one associated w/ your Voice account)
email=userid@gmail.com

# Raw password used for login
password=xyzzy

The credentials in my python script are for the gmail account you will be sending the email to, the phone number in my script is the number that the SMS message will go to, and the credentials in the .gvoice config file are for the Google Voice account that the SMS message will come from.

One thing to note is that this script will forward all emails received as SMS messages, and then delete the emails. So it is probably best to use a dedicated email address solely for this purpose. Google Voice doesn't seem to place a length constraint on their text messages to other GV numbers so message length is apparently not an issue.

Feel free to change anything - especially the formatting of the SMS will be a matter of personal preference. The script uses GPL'd software, so is released under the Gnu Public License available here.

Enjoy!
Dave
 
Thanks!

FWIW, I did a bit more work on the script, so now it works with any SMS-capable phone (not only GV numbers) by sending an email to an address like ##########@your.email.server.com. Here ########## is the phone you want the SMS message sent to (similar to the email to sms gateways from the big boys). Naturally, your.email.server.com isn't a real email server.

Since GV limits the number of SMS messages any one account can send in a 24 hr period, a single implementation of this solution is not really appropriate for a high-volume production environment, but if a couple of people want to give it a try, shoot me a PM and I can give you details about the actual email server I have it running on. It would be good to have a few others besides me use it as well. This would probably be for testing purposes only, although I do run the script on a production machine so its up all the time.

Also, if you'd like a copy of the latest script, let me know and I can email you a copy.

Dave
 
Last edited:
Back
Top