Monday 20 June 2016

Email Header Analyser Using Python

Q:Write a program in Python to analyze email header.

This program simply analyzes a email header stored locally on your machine in a text file and shows information like:
  1. Mime Version
  2. Date
  3. Subject
  4. From
  5. Delivered To
  6. To
This is nothing but a simple file handling and string matching problem. And to make it look insanely simpler, I decided to write the code with the minimum number of lines, and guess what I did manage it in just 15 lines. I did have to remove the exit messages though.


Os: Ubuntu 14.04 LTS
Language: Python
Author: Abhishek Munagekar (Programing Wonders)

Inputs: Just the absolute location of file containing header

Code


import re
def matchre(data,*args):
    for regstr in args:
        matchObj = re.search( regstr+'.*', data, re.M|re.I)
        if matchObj:
            print matchObj.group(0).lstrip().rstrip()
        else:
            print "No ",regstr,"found"
        
print("Email Header Program by Abhishek Munagekar")
filename= raw_input("Enter path for email header file\n");
fo = open(filename, "r") #fo=filehandle
data=fo.read()
matchre(data,"MIME-version","Date:","Subject:","delivered-to:","From:","^to:")
fo.close()


No comments:

Post a Comment