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.
1 2 // First part, email sender, schedule to run every 10 minutes or so 3 // this uses GUID from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604 4 5 from smtplib import SMTP 6 from socket import sslerror #if desired 7 import Guid 8 import os 9 10 guidSubj = Guid.generate() 11 guidBod1 = Guid.generate() 12 guidBod2 = Guid.generate() 13 14 if not os.path.exists("sent.txt"): #check whether the file exists, if not create it 15 fileHandle = open('sent.txt','w') 16 fileHandle.write (guidSubj) 17 fileHandle.close() 18 server = SMTP('smtp.gmail.com',587) 19 server.set_debuglevel(1) # or 1 for verbosity 20 server.ehlo('user@gmail.com') 21 server.starttls() 22 server.ehlo('user@gmail.com') # say hello again 23 server.login('user@gmail.com', 'password') 24 # i have a suspicion that smptlib does not add the required newline dot newline so i do it myself 25 server.sendmail('user@gmail.com', 'user@place.com', "Subject:Ping," + guidSubj + '\n\n' + guidBod1 + '\n\n' + guidBod2 + '\n.\n') 26 # next line generates the ignorable socket.sslerror 27 server.quit() 28 29 // Second email checker/reciever, check every minute or so 30 # This script is a helper to clean POP3 mailboxes 31 # containing malformed mails that hangs MUA's, that 32 # are too large, or whatever... 33 # 34 # It iterates over the non-retrieved mails, prints 35 # selected elements from the headers and prompt the 36 # user to delete bogus messages. 37 # 38 # Written by Xavier Defrang <xavier.defrang@brutele.be> 39 # 40 41 # 42 import getpass, poplib, re, os, fileinput, sys, xmpp 43 44 def sendIM(toAddress=None): 45 46 # Google Talk constants 47 FROM_GMAIL_ID = "user@gmail.com" 48 GMAIL_PASS = "pass" 49 GTALK_SERVER = "talk.google.com" 50 TO_GMAIL_ID = "user@gmail.com" 51 jid=xmpp.protocol.JID(FROM_GMAIL_ID) 52 cl=xmpp.Client(jid.getDomain(),debug=[]) 53 if not cl.connect((GTALK_SERVER,5222)): 54 raise IOError('Can not connect to server.') 55 if not cl.auth(jid.getNode(),GMAIL_PASS): 56 raise IOError('Can not auth with server.') 57 58 cl.send( xmpp.Message( TO_GMAIL_ID ,"Fix your email!" ) ) 59 cl.disconnect() 60 61 # Change this to your needs 62 POPHOST = "131.0.0.1" 63 POPUSER = "user" 64 POPPASS = "pass" 65 66 # How many lines of message body to retrieve 67 MAXLINES = 10 68 69 # Headers we're actually interrested in 70 rx_headers = re.compile(r"^(Subject)") 71 72 try: 73 74 # Connect to the POPer and identify user 75 pop = poplib.POP3(POPHOST) 76 pop.user(POPUSER) 77 78 if not POPPASS: 79 # If no password was supplied, ask for it 80 POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST)) 81 82 # Authenticate user 83 pop.pass_(POPPASS) 84 85 # Get some general informations (msg_count, box_size) 86 stat = pop.stat() 87 88 bye = 0 89 count_del = 0 90 91 #for n in range(stat[0]): 92 93 msgnum = stat[0] 94 95 # Retrieve headers 96 response, lines, bytes = pop.top(msgnum, MAXLINES) 97 98 # Print message info and headers we're interrested in 99 test = "".join(filter(rx_headers.match, lines)) 100 num = test.split(',') 101 out = num[1] 102 103 104 #Read the sent.txt file to get the GUID 105 fileHandle = open ( 'sent.txt' ) 106 sentGuid = fileHandle.readline() 107 print sentGuid 108 fileHandle.close() 109 110 if out == sentGuid: 111 print "They match!! yay" 112 pop.dele(msgnum) 113 print "Message %d marked for deletion" % msgnum 114 count_del += 1 115 #delete the retry.txt and sent.txt file 116 os.remove("retry.txt") 117 os.remove("sent.txt") 118 else: 119 120 #There are no messages yet, so we will increment the retry value 121 if not os.path.exists("retry.txt"): 122 fileHandle = open ( 'retry.txt','a') 123 fileHandle.write('1') 124 fileHandle.close() 125 else: 126 127 fileHandle = open("retry.txt") 128 retryValue = fileHandle.readline() 129 fileHandle.close() 130 #delete the file then recreate with new value 131 os.remove('retry.txt') 132 133 if retryValue != '': 134 retryValue = int(retryValue) + 1 135 out = str(retryValue) 136 fileHandle = open ( 'retry.txt','a') 137 fileHandle.write(out) 138 fileHandle.close() 139 if retryValue > 20 and retryValue <25: 140 sendIM() 141 142 # Summary 143 print "Deleting %d message(s) in mailbox %s@%s" % (count_del, POPUSER, POPHOST) 144 145 # Commit operations and disconnect from server 146 print "Closing POP3 session" 147 pop.quit() 148 149 except poplib.error_proto, detail: 150 151 # Fancy error handling 152 print "POP3 Protocol Error:", detail 153 154 #There are no messages yet, so we will increment the retry value 155 if not os.path.exists("retry.txt"): 156 fileHandle = open ( 'retry.txt','a') 157 fileHandle.write('1') 158 fileHandle.close() 159 else: 160 161 fileHandle = open("retry.txt") 162 retryValue = fileHandle.readline() 163 fileHandle.close() 164 #delete the file then recreate with new value 165 os.remove('retry.txt') 166 167 if retryValue != '': 168 retryValue = int(retryValue) + 1 169 out = str(retryValue) 170 fileHandle = open ( 'retry.txt','a') 171 fileHandle.write(out) 172 fileHandle.close() 173 if retryValue > 20 and retryValue <25: 174 sendIM() 175