Difference between revisions of "CSC220 parseEmail.py"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- <source lang="python"> #! /bin/env python # parseEmamil.py # D. Thiebaut # Oct. 2010 # # An example script that intercepts email messages # and processes them in so…')
 
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
<source lang="python">
 
<source lang="python">
 +
 
#! /bin/env python
 
#! /bin/env python
 
# parseEmamil.py  
 
# parseEmamil.py  
Line 21: 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.)
 +
#
 +
# Main Reference: http://docs.python.org/library/email-examples.html
 
#
 
#
 
import sys
 
import sys
 +
import email
  
#--- get the whole message in data ---
+
try:
data   = sys.stdin.readlines()
+
  #--- 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' )
  
#--- 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 ---
+
  #--- write captured information to file ---
for line in data:
+
  file = open( "/Users/classes/220a/uploads/filtered.mail.txt", "a" )
    line = line.strip()
+
  file.write( "" . join( lines ) )
    if line.find( "Date:" )== 0:
+
  file.close()
        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) ---
+
except:
    if len( line )<=1:
+
  pass
        body = True
 
  
    if body:
 
        message.append( line )
 
        continue
 
  
#--- write captured information to file ---
 
file = open( "/Users/classes/220a/uploads/filtered.mail.txt", "a" )
 
file.write( 'sender = %s\ndate = %s\nsubject = %s\n%s\n\n' %
 
            ( sender, date, subject, '\n'.join( message ) ) )
 
file.close()
 
  
  

Latest revision as of 13:11, 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.)
# 
# Main Reference: http://docs.python.org/library/email-examples.html
#
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