Quick and dirty email server checker, python
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 #if desired import Guid import os guidSubj = Guid.generate() guidBod1 = Guid.generate() guidBod2 = Guid.generate() if not os.path.exists("sent.txt"): #check whether the file exists, if not create it fileHandle = open('sent.txt','w') fileHandle.write (guidSubj) fileHandle.close() server = SMTP('smtp.gmail.com',587) server.set_debuglevel(1) # or 1 for verbosity server.ehlo('user@gmail.com') server.starttls() server.ehlo('user@gmail.com') # say hello again server.login('user@gmail.com', 'password') # i have a suspicion that smptlib does not add the required newline dot newline so i do it myself server.sendmail('user@gmail.com', 'user@place.com', "Subject:Ping," + guidSubj + '\n\n' + guidBod1 + '\n\n' + guidBod2 + '\n.\n') # next line generates the ignorable socket.sslerror server.quit() // Second email checker/reciever, check every minute or so # This script is a helper to clean POP3 mailboxes # containing malformed mails that hangs MUA's, that # are too large, or whatever... # # It iterates over the non-retrieved mails, prints # selected elements from the headers and prompt the # user to delete bogus messages. # # Written by Xavier Defrang <xavier.defrang@brutele.be> # # import getpass, poplib, re, os, fileinput, sys, xmpp def sendIM(toAddress=None): # Google Talk constants 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() # Change this to your needs POPHOST = "131.0.0.1" POPUSER = "user" POPPASS = "pass" # How many lines of message body to retrieve MAXLINES = 10 # Headers we're actually interrested in rx_headers = re.compile(r"^(Subject)") try: # Connect to the POPer and identify user pop = poplib.POP3(POPHOST) pop.user(POPUSER) if not POPPASS: # If no password was supplied, ask for it POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST)) # Authenticate user pop.pass_(POPPASS) # Get some general informations (msg_count, box_size) stat = pop.stat() bye = 0 count_del = 0 #for n in range(stat[0]): msgnum = stat[0] # Retrieve headers response, lines, bytes = pop.top(msgnum, MAXLINES) # Print message info and headers we're interrested in test = "".join(filter(rx_headers.match, lines)) num = test.split(',') out = num[1] #Read the sent.txt file to get the GUID 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 #delete the retry.txt and sent.txt file os.remove("retry.txt") os.remove("sent.txt") else: #There are no messages yet, so we will increment the retry value 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() #delete the file then recreate with new value 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() # Summary print "Deleting %d message(s) in mailbox %s@%s" % (count_del, POPUSER, POPHOST) # Commit operations and disconnect from server print "Closing POP3 session" pop.quit() except poplib.error_proto, detail: # Fancy error handling print "POP3 Protocol Error:", detail #There are no messages yet, so we will increment the retry value 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() #delete the file then recreate with new value 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()