Monday, November 2, 2009

Live @ Edu hotmail active mailboxes

I run a live@edu hotmail system at our school and I needed a way to find out which mailboxes were not longer active (I.E. student hasn't logged in for 365 days). So I put together this little python script to make an SMTP connection on each address load the status into a mysql database.


#######################################
## Written by Matt Brown
## - check hotmail mailbox status active / inactive
## - for live @ edu hotmail accounts
########################################

import smtplib
import fsdb
import time

##-------------------------------------------
def ConnectMySQL(name,debug=0):
fsdb.register_connection(name, 'mydb_ip', 'mydb', 'mydbuser', 'mydbpass')
fsdb.set_debug(debug)
return fsdb
#----------------------------------------------------
def CloseMySQL(name):
fsdb.unregister_connection(name)
#----------------------------------------------------
def RunMySQLQuery(fsdb,query):
return fsdb.query(query,None)
#----------------------------------------------------

count = 0
try:
name ="email"
fsdb = ConnectMySQL(name)
email_accounts = RunMySQLQuery(fsdb,"select id,email from addresses_table WHERE active_mailbox='0'")

s = smtplib.SMTP('pamx1.hotmail.com','25','localhost')
s.ehlo('verify')
s.mail('admin@mydomain.com')

for user in email_accounts:
mbstatus = s.rcpt(user[1])
if mbstatus[0] == 550:
print user, "inactive"
RunMySQLQuery(fsdb,"update addresses_table set active_mailbox=0,mailbox_check=NOW() WHERE id="+str(user[0]))
elif mbstatus[0] == 250:
print user, "active"
RunMySQLQuery(fsdb,"update addresses_table set mailbox_check=NOW() WHERE id="+str(user[0]))
count = count + 1
else:
print mbstatus[0], mbstatus

# hotmail only allows 10 recipiants
if count == 9:
count = 0
time.sleep(2) # pause so we don't get black listed
s.rset
s = smtplib.SMTP('pamx1.hotmail.com','25','localhost')
s.ehlo('verify')
s.mail('admin@mydomain.com')

CloseMySQL(name)

except Exception,e:
CloseMySQL(name)
print e

1 comment:

  1. This script should be run from a system on the live whitelist otherwise the ip will be blocked.

    ReplyDelete