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:
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:
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
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