[FrontPage] [TitleIndex] [WordIndex]

bounce address.py

#!/usr/bin/env python

import email.Parser
import email.Header

import os
import sys
import re
import string

def main():
  if len(sys.argv)==1:
    mailFile = sys.stdin
  else:
    mailFile=open(sys.argv[1],"rb")

  p=email.Parser.Parser()

  msg=p.parse(mailFile)
  mailFile.close()

  rec_bounce1 = re.compile("55[0-9] <(.*?@.*?)>")
  rec_bounce2 = re.compile("<(.*?@.*?)>:")

  bounce_matches = []
  bounce_matches.append(rec_bounce1)
  bounce_matches.append(rec_bounce2)

  if msg.is_multipart():
    body = msg.get_payload(0).as_string()
  else:
    body = msg.get_payload()

  for rec in bounce_matches:

    result  = rec.search(body)

    if result != None:
      print result.group(1)
      sys.exit(0)

# If we got here no address was found.
  print "ERROR"
  sys.exit(1)

2007-02-24 17:18