Difference between revisions of "CSC220 parseEmail.py"
(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: |
− | + | #--- 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(): | |
− | body = | + | 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 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
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