Difference between revisions of "CSC220 parseEmail.py"

From dftwiki3
Jump to: navigation, search
Line 22: Line 22:
 
#
 
#
 
# This script appends the date, subject, sender, and body of the
 
# This script appends the date, subject, sender, and body of the
# message to a file in ~220a/uploads called filtered.mail.txt
+
# message to a file in ~220a/uploads called filtered.mail.txt.
 +
# (uploads does not need to be world writable.)
 
#
 
#
 
import sys
 
import sys
 +
import email
  
 
try:
 
try:
 
   #--- get the whole message in data ---
 
   #--- get the whole message in data ---
   data    = sys.stdin.readlines()
+
   em = email.message_from_file(sys.stdin) # Read message from Std Input
 +
  subject = em.get('Subject')            # Get the subject from em object
 +
  date    = em.get('Date' )
 +
  sender  = em.get('From' )
  
   #--- prepare fields of interest ---
+
   lines = "Sender: %s\nDate: %s\nSubject: %s\n\n" % ( sender, date, subject )
  date    = sender = subject = None
+
  for part in em.walk():
  body    = False
+
      msgType = repr( part.get_content_type() )
  message = []
+
      body    = repr( part.get_payload() )
 +
      lines += "Type: %s\n" % msgType
 +
      lines += "Body: %s\n\n" % body
  
 
  #--- scan all the lines ---
 
  for line in data:
 
      line = line.strip()
 
      if line.find( "Date:" )== 0:
 
          date = line.split( 'Date:' )[1]
 
          continue
 
      if line.find( "From:" )== 0:
 
          sender = line.split( 'm:' )[1]
 
          continue
 
      if line.find( "Subject:" )== 0:
 
          subject = line.split( 'Subject:' )[1]
 
          continue
 
 
      #--- first blank line is beginning of message (usually) ---
 
      if len( line )<=1:
 
          body = True
 
         
 
      if body:
 
          message.append( line )
 
          continue
 
  
 
   #--- write captured information to file ---
 
   #--- write captured information to file ---
 
   file = open( "/Users/classes/220a/uploads/filtered.mail.txt", "a" )
 
   file = open( "/Users/classes/220a/uploads/filtered.mail.txt", "a" )
   file.write( 'sender = %s\ndate = %s\nsubject = %s\n%s\n\n' %
+
   file.write( "" . join( lines ) )
              ( sender, date, subject, '\n'.join( message ) ) )
 
 
   file.close()
 
   file.close()
  
 
except:
 
except:
 
   pass
 
   pass
 +
  
  

Revision as of 10:43, 21 October 2010

--D. Thiebaut 22:34, 5 October 2010 (UTC)


#! /bin/env python
# parseEmamil.py 
# D. Thiebaut
# Oct. 2010
#
# An example script that intercepts email messages
# and processes them in some fashion.
#
# Setup:
# in 220a account, create .forward file with this single line
#
# "|/usr/bin/python /Users/classes/220a/bin/parseMail.py", \220a
#
# First part indicates that message body should be 'piped' to the
# python program parseMail.py in ~220a/bin.  Second part says that
# a copy of the message should be kept in mailbox of User 220a.
#
# This script appends the date, subject, sender, and body of the
# message to a file in ~220a/uploads called filtered.mail.txt.
# (uploads does not need to be world writable.)
#
import sys
import email

try:
   #--- get the whole message in data ---
   em = email.message_from_file(sys.stdin) # Read message from Std Input 
   subject = em.get('Subject')             # Get the subject from em object 
   date    = em.get('Date' )
   sender  = em.get('From' )

   lines = "Sender: %s\nDate: %s\nSubject: %s\n\n" % ( sender, date, subject )
   for part in em.walk():
      msgType = repr( part.get_content_type() )
      body    = repr( part.get_payload() )
      lines += "Type: %s\n" % msgType 
      lines += "Body: %s\n\n" % body 


   #--- write captured information to file ---
   file = open( "/Users/classes/220a/uploads/filtered.mail.txt", "a" )
   file.write( "" . join( lines ) )
   file.close()

except:
   pass