Monday, September 5, 2011

Sending Text Messages with Python

One of the neat things I like to do with my Astro-Imaging scripts is to have the ability to send periodic status messages to my cell phone. With this capability, if I am away from my imaging computer, I can always know how imaging is progressing. Also, I can have my script send any error messages that crop up so that I may immediately return to my imaging setup if needed.

The key to sending text messages is contained in this List of SMS Gateways that contains e-mail addresses for each of the major cell phone carriers. Basically, all you need to do is send an e-mail message to the domain that applies to your cell phone carrier with your phone's ten-digit number as the address. As the Wikipedia page explains, you can only send simple text messages to the SMS gateway. The following Python code listing shows how to send the e-mail message programmatically. Obviously, you will need to know the outgoing e-mail domain, your user name and password for accessing your personal e-mail service provider to make the code work.

The listing shown below is coded for my particular service carrier (AT&T Wireless). To personalize the code for your particular setup, just modify the five constants defined prior to the class declaration. The source file corresponding to this listing can be downloaded from cTexting.zip.
from smtplib import SMTP
import datetime
import socket
import traceback

SMTP_DOMAIN = r'<OUTGOING EMAIL DOMAIN>'
SMTP_USER   = r'<USER NAME>'
SMTP_PASS   = r'<PASSWORD>'
FROM_ADDR   = r'AstroPhoto Update <anyone@gmail.com>'
TO_ADDR     = r'<TEN-DIGIT CELL NUMBER>@txt.att.net'

socket.setdefaulttimeout(10)

##--------------------------------------------------------------------------------
## Class: cTextMsg
##------------------------------------------------------------------------------
class cTextMsg:
    __objectName = ''

    def __init__(self):
        pass
        
    def set_objectName(self, value):
        self.__objectName = value
        return
    
    def get_objectName(self):
        return self.__objectName

    def sendSMSMessage(self,message):
        smtp = SMTP()
        smtp.set_debuglevel(0)
        
        subject = self.get_objectName()
        date = datetime.datetime.now().strftime('%d/%m/%Y %H:%M')

        msg = 'From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s' % \
                (FROM_ADDR,TO_ADDR,subject,date,message)
        
        try:
            smtp.connect(SMTP_DOMAIN,0)
            smtp.login(SMTP_USER,SMTP_PASS)
            smtp.sendmail(FROM_ADDR,TO_ADDR,msg)
            print "SMS Message Sent Successfully"
            smtp.quit()
        except socket.timeout:
            print "ERROR: Timeout sending SMS Message"
        except:
            print "ERROR: Unable to send SMS Message"
            print '!'*60
            print traceback.format_exc()
        
##
##    END OF 'cTextMsg' Class
##

if __name__ == '__main__':
    
    email_msg = cTextMsg()
    email_msg.set_objectName('M31')
    print 'Object Name: ' + email_msg.get_objectName()
    email_msg.sendSMSMessage('This is a sample message from an imaging session.')

No comments:

Post a Comment