<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: checker code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 00:32:23 GMT</pubDate>
    <description>DZone Snippets: checker code</description>
    <item>
      <title>Quick and dirty email server checker, python</title>
      <link>http://snippets.dzone.com/posts/show/4024</link>
      <description>// description of your code here&lt;br /&gt;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.  &lt;br /&gt;&lt;code&gt;&lt;br /&gt;// First part, email sender, schedule to run every 10 minutes or so&lt;br /&gt;// this uses GUID from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604&lt;br /&gt;&lt;br /&gt;from smtplib import SMTP&lt;br /&gt;from socket import sslerror         #if desired&lt;br /&gt;import Guid&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;guidSubj = Guid.generate()&lt;br /&gt;guidBod1 = Guid.generate()&lt;br /&gt;guidBod2 = Guid.generate()&lt;br /&gt;&lt;br /&gt;if not os.path.exists("sent.txt"):   #check whether the file exists, if not create it&lt;br /&gt;    fileHandle = open('sent.txt','w')&lt;br /&gt;    fileHandle.write (guidSubj)&lt;br /&gt;    fileHandle.close()&lt;br /&gt;    server = SMTP('smtp.gmail.com',587)&lt;br /&gt;    server.set_debuglevel(1) # or 1 for verbosity&lt;br /&gt;    server.ehlo('user@gmail.com')&lt;br /&gt;    server.starttls()&lt;br /&gt;    server.ehlo('user@gmail.com')  # say hello again&lt;br /&gt;    server.login('user@gmail.com', 'password')&lt;br /&gt;    # i have a suspicion that smptlib does not add the required newline dot newline so i do it myself&lt;br /&gt;    server.sendmail('user@gmail.com', 'user@place.com', "Subject:Ping," + guidSubj + '\n\n' + guidBod1 + '\n\n' + guidBod2 + '\n.\n')&lt;br /&gt;    # next line generates the ignorable socket.sslerror&lt;br /&gt;    server.quit()&lt;br /&gt;&lt;br /&gt;// Second email checker/reciever, check every minute or so&lt;br /&gt;# This script is a helper to clean POP3 mailboxes&lt;br /&gt;# containing malformed mails that hangs MUA's, that &lt;br /&gt;# are too large, or whatever...&lt;br /&gt;#&lt;br /&gt;# It iterates over the non-retrieved mails, prints&lt;br /&gt;# selected elements from the headers and prompt the &lt;br /&gt;# user to delete bogus messages.&lt;br /&gt;#&lt;br /&gt;# Written by Xavier Defrang &lt;xavier.defrang@brutele.be&gt;&lt;br /&gt;# &lt;br /&gt;&lt;br /&gt;# &lt;br /&gt;import getpass, poplib, re, os, fileinput, sys, xmpp&lt;br /&gt;&lt;br /&gt;def sendIM(toAddress=None):&lt;br /&gt;    &lt;br /&gt;    # Google Talk constants&lt;br /&gt;    FROM_GMAIL_ID = "user@gmail.com"&lt;br /&gt;    GMAIL_PASS = "pass"&lt;br /&gt;    GTALK_SERVER = "talk.google.com"&lt;br /&gt;    TO_GMAIL_ID = "user@gmail.com"&lt;br /&gt;    jid=xmpp.protocol.JID(FROM_GMAIL_ID)&lt;br /&gt;    cl=xmpp.Client(jid.getDomain(),debug=[])&lt;br /&gt;    if not cl.connect((GTALK_SERVER,5222)):&lt;br /&gt;        raise IOError('Can not connect to server.')&lt;br /&gt;    if not cl.auth(jid.getNode(),GMAIL_PASS):&lt;br /&gt;        raise IOError('Can not auth with server.')&lt;br /&gt;&lt;br /&gt;    cl.send( xmpp.Message( TO_GMAIL_ID ,"Fix your email!" ) )&lt;br /&gt;    cl.disconnect()&lt;br /&gt;&lt;br /&gt;# Change this to your needs&lt;br /&gt;POPHOST = "131.0.0.1"&lt;br /&gt;POPUSER = "user"&lt;br /&gt;POPPASS = "pass"&lt;br /&gt;&lt;br /&gt;# How many lines of message body to retrieve&lt;br /&gt;MAXLINES = 10&lt;br /&gt;&lt;br /&gt;# Headers we're actually interrested in&lt;br /&gt;rx_headers  = re.compile(r"^(Subject)")&lt;br /&gt;&lt;br /&gt;try:&lt;br /&gt;&lt;br /&gt;    # Connect to the POPer and identify user&lt;br /&gt;    pop = poplib.POP3(POPHOST)&lt;br /&gt;    pop.user(POPUSER)&lt;br /&gt;&lt;br /&gt;    if not POPPASS:&lt;br /&gt;        # If no password was supplied, ask for it&lt;br /&gt;        POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST))&lt;br /&gt;&lt;br /&gt;    # Authenticate user&lt;br /&gt;    pop.pass_(POPPASS)&lt;br /&gt;&lt;br /&gt;    # Get some general informations (msg_count, box_size)&lt;br /&gt;    stat = pop.stat()&lt;br /&gt;&lt;br /&gt;    bye = 0&lt;br /&gt;    count_del = 0&lt;br /&gt;    &lt;br /&gt;    #for n in range(stat[0]):&lt;br /&gt;&lt;br /&gt;    msgnum = stat[0]&lt;br /&gt;&lt;br /&gt;    # Retrieve headers&lt;br /&gt;    response, lines, bytes = pop.top(msgnum, MAXLINES)&lt;br /&gt;&lt;br /&gt;    # Print message info and headers we're interrested in&lt;br /&gt;    test = "".join(filter(rx_headers.match, lines))&lt;br /&gt;    num = test.split(',')&lt;br /&gt;    out = num[1]&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;    #Read the sent.txt file to get the GUID&lt;br /&gt;    fileHandle = open ( 'sent.txt' )&lt;br /&gt;    sentGuid = fileHandle.readline()&lt;br /&gt;    print sentGuid&lt;br /&gt;    fileHandle.close()&lt;br /&gt;    &lt;br /&gt;    if out == sentGuid:&lt;br /&gt;        print "They match!! yay"&lt;br /&gt;        pop.dele(msgnum)&lt;br /&gt;        print "Message %d marked for deletion" % msgnum&lt;br /&gt;        count_del += 1&lt;br /&gt;        #delete the retry.txt and sent.txt file&lt;br /&gt;        os.remove("retry.txt")&lt;br /&gt;        os.remove("sent.txt")&lt;br /&gt;    else:&lt;br /&gt;&lt;br /&gt;        #There are no messages yet, so we will increment the retry value&lt;br /&gt;        if not os.path.exists("retry.txt"):&lt;br /&gt;            fileHandle = open ( 'retry.txt','a')&lt;br /&gt;            fileHandle.write('1')&lt;br /&gt;            fileHandle.close()&lt;br /&gt;        else:&lt;br /&gt;        &lt;br /&gt;        fileHandle = open("retry.txt")&lt;br /&gt;        retryValue = fileHandle.readline()&lt;br /&gt;        fileHandle.close()&lt;br /&gt;        #delete the file then recreate with new value&lt;br /&gt;        os.remove('retry.txt')&lt;br /&gt;&lt;br /&gt;        if retryValue != '':&lt;br /&gt;            retryValue = int(retryValue) + 1&lt;br /&gt;            out = str(retryValue)&lt;br /&gt;            fileHandle = open ( 'retry.txt','a')&lt;br /&gt;            fileHandle.write(out)&lt;br /&gt;            fileHandle.close()&lt;br /&gt;            if retryValue &gt; 20 and retryValue &lt;25:&lt;br /&gt;                sendIM()&lt;br /&gt;&lt;br /&gt;   # Summary&lt;br /&gt;    print "Deleting %d message(s) in mailbox %s@%s" % (count_del, POPUSER, POPHOST)&lt;br /&gt;&lt;br /&gt;    # Commit operations and disconnect from server&lt;br /&gt;    print "Closing POP3 session"&lt;br /&gt;    pop.quit()&lt;br /&gt;&lt;br /&gt;except poplib.error_proto, detail:&lt;br /&gt;&lt;br /&gt;    # Fancy error handling&lt;br /&gt;    print "POP3 Protocol Error:", detail&lt;br /&gt;&lt;br /&gt;    #There are no messages yet, so we will increment the retry value&lt;br /&gt;    if not os.path.exists("retry.txt"):&lt;br /&gt;        fileHandle = open ( 'retry.txt','a')&lt;br /&gt;        fileHandle.write('1')&lt;br /&gt;        fileHandle.close()&lt;br /&gt;    else:&lt;br /&gt;        &lt;br /&gt;        fileHandle = open("retry.txt")&lt;br /&gt;        retryValue = fileHandle.readline()&lt;br /&gt;        fileHandle.close()&lt;br /&gt;        #delete the file then recreate with new value&lt;br /&gt;        os.remove('retry.txt')&lt;br /&gt;&lt;br /&gt;        if retryValue != '':&lt;br /&gt;            retryValue = int(retryValue) + 1&lt;br /&gt;            out = str(retryValue)&lt;br /&gt;            fileHandle = open ( 'retry.txt','a')&lt;br /&gt;            fileHandle.write(out)&lt;br /&gt;            fileHandle.close()&lt;br /&gt;            if retryValue &gt; 20 and retryValue &lt;25:&lt;br /&gt;                sendIM()&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 May 2007 18:26:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4024</guid>
      <author>mellerbeck (Michael Ellerbeck)</author>
    </item>
  </channel>
</rss>
