simple script to read and output Mailbox body to file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck Amadi

    #16
    Re: simple script to read and output Mailbox body to file.

    Hi again

    I get output desired .Now to parse the fileobject and all body messages to an
    external File named SurveyResults.t xt
    Is this the correct way to go about it .

    for mail in mbox:
    # body = mail.get_payloa d()
    # bodies.append(b ody)
    # msg = mail.message_fr om_file(mail)
    print 'mail'
    print mail['Subject']
    print mail.get_conten t_type()#text/plain
    print mail.get_payloa d()
    # break # just look at one message

    # The File SurveyResults.t xt must all ready exist.
    output = open("SurveyRes ults.txt", 'w') # mode 'w' means open the file for
    writ-ing (any data already in the file will be erased)
    mailout = mail.get_payloa d()
    output. writelines(mail out)

    output. close() # this is at end of the script

    Here's my error but I thought mail is my file

    Traceback (most recent call last):
    File "getSurveyMailR ev2.py", line 37, in ?
    mailout = mail.get_payloa d()
    NameError: name 'mail' is not defined




    Comment

    • Chuck Amadi

      #17
      Re: simple script to read and output Mailbox body to file.


      Hi please could someone point to what I have done wrong as the script runs
      with no errors but the file I am trying to parse the print statement output
      too instead of just the screen/command line to another file.

      I have digested the Input and Output and text formatting docs and I read
      something about using stdout to a file . Can someone point me in the right
      direction.

      Cheers my code below.

      ############### ############### ############### ############### ###
      ## This script will open and parse email messages body content.
      ## This Python script will reside on Mail Server on ds9:
      ## Emails are all plain/text you could just write the following
      ## Which will leave a list of strings , each one a message body.
      ## The Survey User is testwws and the .procmailrc file folder is
      ## Survey . i.e /home/testwws/Mail/inbox/Survey .
      ############### ############### ############### ############### ###
      ## file:getSurveyM ail.py Created : 06/06/04 Amended date: 09/06/04
      ## Revision 1. make copy to continue called getSurveyMailRe v2.py
      ############### ############### ############### ############### ###

      #The following line makes it run itself(executab le script on UN*X)
      #!/usr/bin/env python

      import sys
      import os
      import email
      import mailbox
      import StringIO

      # Open the testwws user mailbox (tmp user chuck)
      # fp denotes factory paraemeter
      # mode can be 'r' when the file will only be read, 'w' for only writing
      #(an existing file with the same name will be erased), and 'a' opens the file
      # for appending; any data written to the file is automatically added to the end.
      # 'r+' opens the file for both reading and writing. The mode.

      # The File SurveyResults.t xt must all ready exist.

      #mailout = open("/home/chuck/pythonScript/SurveyResults.t xt") # mode 'w' means open the file for writ-ing
      # (any data already in the file will be erased)
      #output.writeli nes(mailout)
      mailout = file("/home/chuck/pythonScript/SurveyResults.t xt")

      # open() returns a file object, and is most commonly used with two arguments:
      # "open(filen ame, mode)".
      fp = open("/home/chuck/pythonScript/testbox")

      # message_from_fi le returns a message object struct tree from an
      # open file object.

      mbox = mailbox.UnixMai lbox(fp, email.message_f rom_file)
      # list of body messages.
      bodies = []

      # mail is the file object
      for mail in mbox:
      print 'mail'
      print mail['Subject']
      print mail.get_conten t_type()#text/plain
      print mail.get_payloa d()


      # First open the testbox file to read(r) and write(w)to the SurveyResults.t xt
      #fp = open("testbox", "r")
      mailout = open("/home/chuck/pythonScript/SurveyResults.t xt","w")

      # Read the testbox file into a list then copy to
      # new file.
      for mail in fp.readlines():
      mailout.write(m ail)

      print "testbox file copied...to SurveyResults.t xt"

      # Now close the files
      fp.close()
      mailout.close()

      #mailout.close to close it and free up any system resources taken up by the open file.
      # After calling output.close(), attempts to use the file object will automatically fail.





      Comment

      Working...