simple script to read and parse mailbox

Discussion in 'Python' started by Chuck Amadi, Jun 7, 2004.

  1. Chuck Amadi

    Chuck Amadi Guest

    Hi fishboy I have tried this snippet the output only generates an empty list
    [].
    Have goto to add - msg = email.message_from_file(fp) thus print msg

    The print output is

    chuck@sevenofnine:pythonScript>From nobody Mon Jun 7 15:44:29 2004

    How do I get all the body messages I have tried print bodies but just get an
    empty list []. I exspect because that's all what's there So using this script
    can I use the /home/testwwws/Mail/work # what I do when I scan+ to check the
    contents of the mail folder.

    I need the body contents that resides in /home/testwwws/Mail/work Mail Folder.

    Here's the script. Cheers


    import email
    import mailbox
    fp = open("/var/spool/mail/chucka")
    mbox = mailbox.UnixMailbox(fp, email.message_from_file)
    bodies = []
    for msg in mbox:
    body = msg.get_payload()
    bodies.append(body)

    ###########################################################

    chuck@sevenofnine:~/pythonScript> cat getSurveyMail.py
    ###############################################################
    ## 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:getSurveyMail.py Created : 06/06/04 Amended date: 07/06/04
    ###############################################################

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

    import sys
    import os
    import email
    import mailbox

    # Open the testwws user mailbox (tmp user chuck)
    # fp denotes factory paremeter

    output =('/tmp/SurveyResults','w+a')
    fp = open("/var/spool/mail/chuck")

    #fp = open("/var/spool/mail/testwws")


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

    mbox = mailbox.UnixMailbox(fp, email.message_from_file)
    # list of body messages.
    bodies = []

    # for loop iterates through the msg in the mbox(mailbox).
    # Subparts of messages can be accessed via the -
    # get_payload() method will return a string object.
    # If it is multipart, use the "walk" method to iterate through each part and
    the
    # get the payload.In our case it's not multipart So ignore.
    # for part in msg.walk():
    # msg = part.get_payload()
    # # do something(print)

    for msg in mbox:
    body = msg.get_payload()
    bodies.append(body)
    # Print to screen for testing purposes.
    # print the bodies list of the messages.
    # print body - not defined
    print bodies # produces an emtpy list []
     
    Chuck Amadi, Jun 7, 2004
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    5
    Views:
    657
    Kevin Altis
    Aug 25, 2003
  2. Chuck Amadi
    Replies:
    4
    Views:
    712
    William Park
    Jun 6, 2004
  3. chuck amadi

    simple script to read and parse mailbox

    chuck amadi, Jun 5, 2004, in forum: Python
    Replies:
    6
    Views:
    534
    Chuck Amadi
    Jun 7, 2004
  4. Chuck Amadi

    simple script to read and parse mailbox

    Chuck Amadi, Jun 7, 2004, in forum: Python
    Replies:
    0
    Views:
    342
    Chuck Amadi
    Jun 7, 2004
  5. Chuck Amadi
    Replies:
    16
    Views:
    2,995
    Chuck Amadi
    Jun 9, 2004
Loading...

Share This Page