CSC220 parseEmail.py

From dftwiki3
Revision as of 13:11, 21 October 2010 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--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