pythonmbox

Extract the last mail from a mbox file


I have a mbox file that contains many emails,I tried this code that typically reads all messages,what i want is to read and print only the last email of the mbox file and store it alone in another mbox file. This is the simple code that I wrote:

import mailbox

for msg in mailbox.mbox('C:\\Users\\hmk\Desktop\\PFE 2019\\ML\\MachineLearningPhishing-master\\MachineLearningPhishing-master\\code\\resources\\mboxfile.mbox'):
print(msg)

Solution

  • Your code has a syntax error; the line after the for loop should be indented. But actually the solution to your problem is to move it outside the loop. Then you just need to put something else inside the loop.

    import mailbox
    
    for msg in mailbox.mbox('C:\\Users\\hmk\Desktop\\PFE 2019\\ML\\MachineLearningPhishing-master\\MachineLearningPhishing-master\\code\\resources\\mboxfile.mbox'):
        pass
    # We are now outside the loop, and `msg` contains the last message
    print(msg)
    

    Of course, a better fix is to not loop at all.

    messages = mailbox.mbox('C:\\Users\\hmk\Desktop\\PFE 2019\\ML\\MachineLearningPhishing-master\\MachineLearningPhishing-master\\code\\resources\\mboxfile.mbox')
    print(messages[messages.keys()[-1]])
    

    The above assumes you are on a Python version which is new enough to keep dictionaries sorted in insertion order. If not, you probably do need the loop after all.

    As a final aside, probably don't hardcode absolute file paths. Make your program accept a file name argument, so you can run it on any mbox file in any directory.

    import mailbox
    import sys
    
    messages = mailbox.mbox(sys.argv[1])
    print(messages[messages.keys()[-1]])
    

    Call it like

    python3 lastmsg.py C:\Users\hmkDesktop\PFE 2019\ML\MachineLearningPhishing-master\MachineLearningPhishing-master\code\resources\mboxfile.mbox >last
    

    Obviously, a production script should have some error checking and help etc but I leave those as an exercise.