extract gmail message body from mbox file

i am trying to do this

but also extract the message of each email in gmail
right now just getting the sender and reciever data and not the actual content of the message

not sure how to do this either in this code or via this method-
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
I have looked into your 2 links, and it was not clear to me how to help you, so I search again on the web and found this link:
It seems to be good and clarifies that you will need to use a recursive approach to load the messages (body).
import mailbox

def showMbox(mboxPath):
    box = mailbox.mbox(mboxPath)
    for msg in box:
        print msg['Subject']

        print '**********************************'

def showPayload(msg):
    payload = msg.get_payload()

    if msg.is_multipart():
        div = ''
        for subMsg in payload:
            print div
            div = '------------------------------'
        print msg.get_content_type()
        print payload[:200]

if __name__ == '__main__':

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
finnstoneAuthor Commented:
thanks Walter. is that python?
Walter RitzelSenior Software EngineerCommented:
Yes, it is.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

finnstoneAuthor Commented:
will this export the messages into csv. thats what i was looking for
Walter RitzelSenior Software EngineerCommented:
This give you the structure on how to get and read a mbox file. Now, you just need to write the piece to write it down to a CSV. Unless I did not understood and you want us to code all for you.
finnstoneAuthor Commented:
yes I do need help doing that, much appreciated!
Walter RitzelSenior Software EngineerCommented:
Well, let's do something: the purpose here is not deliver a full solution to you, but help you to learn how to solve the issue. So, why don't you publish your code so far, with samples and such and we work on top of it together?
finnstoneAuthor Commented:
wow amazing
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.