Jump to content

SMS notification via Python + Email


i.have.rewt
 Share

Recommended Posts

Hey people I'm new to the forums, hope to learn lots here.

In my debut I'd like to share some code I've created.

here's the thing.. I'm looking for changes to a website and I hate to have to keep on going back to check it manually..plus there's a bunch of other sites I need to check. This script can be the foundation of some other great implementations.

So when there's a change it sends me a text message to my phone. The neat thing is that all I have to do is send the message to an email address like: phonenumber@vtext.com

Now this may not apply to all carriers of course but it definitely works for Verizon.

I also have this set up to run on a schedule using my home server --since it's on 24/7, I can be sure the script works. When it doesn't I'm notified via the operating system that the job failed...but that's another can of worms.

This can be easily modified to do other neat things and I recommend using SSH tunneling whenever you can to improve security. Or if your email service allows it, to use the secure email connection function from Python. (Please please please use Google or python's interactive mode dir/help commands..but I will help if something more difficult arises)

Requires: phone (and a carrier that allows sms via email) + Python + email account (that allows SMTP access)

#! /bin/usr/env/ python

import urllib
import smtplib
from email.mime.text import MIMEText

#Use your email address
addr_from = 'myaddr@gmail.com'
pw = 'mypass'

#Phone number including area code
number = '1112223333'

#Change this domain to your carrier's text messaging service domain
domain = 'vtext.com'

#Your email carrier for using their send mail service
SMTPserver = 'smtp.gmail.com'

urlToCheck = 'www.change_this.com'

###### You shouldn't need to change anything below this line

addr_to = number + '@' + domain
cached_name = 'cached.html'

try: f = open(cached_name, 'r')
except: pass

try: lines = f.readlines()
except: lines = []

try: f.close()
except: pass

def sendit(msg):
    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    s = smtplib.SMTP(SMTPserver)

    print s.ehlo()
    print s.starttls()
    print s.ehlo()

    print s.login(addr_from, pw)
    print s.sendmail(addr_from, [addr_to], msg.as_string())
    print s.quit()
    
def main():    
    spb_page = urllib.urlopen(urlToCheck)
    wwwlines = spb_page.readlines()

    if lines and not (lines == wwwlines):
        txt = 'Testing page has changed!'
        print txt
        msg = MIMEText(txt)
        sendit(msg)
        f = open(cached_name, 'w')
        f.writelines(wwwlines)
        f.close()

    if not lines:
        f = open(cached_name, 'w')
        f.writelines(wwwlines)
        f.close()
        
if __name__ == '__main__': main()

Happy hacking!

Link to comment
Share on other sites

Since I am lazy and have not read your code, I shall ask some questions.

Did you use that cool module to open web pages and then put it into a text file, then later check the website again and dump it in a file or something and compare them? Then did you use that other cool email module to send it to an email address if it's different?

If that's not how you did it, then I'm gonna make one like that.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...