Wednesday, 26 August 2015

Python EML file viewer (simple version)

Sometimes, employees passes eml file to me for a reference or etc.
Unfortunately, I don't have an eml viewer....

So I just coded simply convert from eml file to html for only plain/text and that is in the base64.
When I searched python module for an eml converter, I am able to find out the "email" module.
But I need only simple version. :)

I hope it is helping your working. :)



 import re, base64  
   
 filename = "./1.eml"  
   
 num_lines = sum(1 for line in open(filename))  
   
 S = ""  
 with open(filename, "r") as f:  
   for i in range(0, num_lines-1):  
     if (re.findall("Content-Type: ", f.readline())):  
       i = i + 2  
       f.readline()  
       #print (f.readline())  
       if(re.findall("Content-Transfer-Encoding: base64", f.readline())):  
         f.readline()  
         while(1):  
           tmp = f.readline()+f.readline()  
           if (re.findall("\n\n", tmp)):  
             break  
           S = S+tmp  
 with open(filename+"_convert.html", "w") as con_f:  
   con_f.write("<b>CONVERT: ONLY PLAIN/TEXT</b><br /><br />\n")  
   con_f.write(base64.b64decode(S))