// description of your code here
This sends an email from a gmail account with a GUID for the subject to another email account, and then writes this GUID to file. On the other end, the other half of the program checks the GUID in the recieved message and matches it to the GUID in the file to verify that the message has been recieved. I schedule the sender to run every ten minutes, and the reciever/checker to run every minute. The reciever has a threshold value of 20 so if it has checked 20 times and not recieved the email it sends an IM to my gmail account. Whew is that contrived or what.
// First part, email sender, schedule to run every 10 minutes or so
// this uses GUID from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604
from smtplib import SMTP
from socket import sslerror
import Guid
import os
guidSubj = Guid.generate()
guidBod1 = Guid.generate()
guidBod2 = Guid.generate()
if not os.path.exists("sent.txt"):
fileHandle = open('sent.txt','w')
fileHandle.write (guidSubj)
fileHandle.close()
server = SMTP('smtp.gmail.com',587)
server.set_debuglevel(1)
server.ehlo('user@gmail.com')
server.starttls()
server.ehlo('user@gmail.com')
server.login('user@gmail.com', 'password')
server.sendmail('user@gmail.com', 'user@place.com', "Subject:Ping," + guidSubj + '\n\n' + guidBod1 + '\n\n' + guidBod2 + '\n.\n')
server.quit()
// Second email checker/reciever, check every minute or so
import getpass, poplib, re, os, fileinput, sys, xmpp
def sendIM(toAddress=None):
FROM_GMAIL_ID = "user@gmail.com"
GMAIL_PASS = "pass"
GTALK_SERVER = "talk.google.com"
TO_GMAIL_ID = "user@gmail.com"
jid=xmpp.protocol.JID(FROM_GMAIL_ID)
cl=xmpp.Client(jid.getDomain(),debug=[])
if not cl.connect((GTALK_SERVER,5222)):
raise IOError('Can not connect to server.')
if not cl.auth(jid.getNode(),GMAIL_PASS):
raise IOError('Can not auth with server.')
cl.send( xmpp.Message( TO_GMAIL_ID ,"Fix your email!" ) )
cl.disconnect()
POPHOST = "131.0.0.1"
POPUSER = "user"
POPPASS = "pass"
MAXLINES = 10
rx_headers = re.compile(r"^(Subject)")
try:
pop = poplib.POP3(POPHOST)
pop.user(POPUSER)
if not POPPASS:
POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST))
pop.pass_(POPPASS)
stat = pop.stat()
bye = 0
count_del = 0
msgnum = stat[0]
response, lines, bytes = pop.top(msgnum, MAXLINES)
test = "".join(filter(rx_headers.match, lines))
num = test.split(',')
out = num[1]
fileHandle = open ( 'sent.txt' )
sentGuid = fileHandle.readline()
print sentGuid
fileHandle.close()
if out == sentGuid:
print "They match!! yay"
pop.dele(msgnum)
print "Message %d marked for deletion" % msgnum
count_del += 1
os.remove("retry.txt")
os.remove("sent.txt")
else:
if not os.path.exists("retry.txt"):
fileHandle = open ( 'retry.txt','a')
fileHandle.write('1')
fileHandle.close()
else:
fileHandle = open("retry.txt")
retryValue = fileHandle.readline()
fileHandle.close()
os.remove('retry.txt')
if retryValue != '':
retryValue = int(retryValue) + 1
out = str(retryValue)
fileHandle = open ( 'retry.txt','a')
fileHandle.write(out)
fileHandle.close()
if retryValue > 20 and retryValue <25:
sendIM()
print "Deleting %d message(s) in mailbox %s@%s" % (count_del, POPUSER, POPHOST)
print "Closing POP3 session"
pop.quit()
except poplib.error_proto, detail:
print "POP3 Protocol Error:", detail
if not os.path.exists("retry.txt"):
fileHandle = open ( 'retry.txt','a')
fileHandle.write('1')
fileHandle.close()
else:
fileHandle = open("retry.txt")
retryValue = fileHandle.readline()
fileHandle.close()
os.remove('retry.txt')
if retryValue != '':
retryValue = int(retryValue) + 1
out = str(retryValue)
fileHandle = open ( 'retry.txt','a')
fileHandle.write(out)
fileHandle.close()
if retryValue > 20 and retryValue <25:
sendIM()