nookonee
nookonee

Reputation: 921

Open mail in python

I'm trying to do a python code to open a Mailbox and reading them..

All I can do for now is to do a connection to a mailbox (a gmail or a hotmail for example) and getting my mail but I got something like this :

I think it's the header of the mail.

Delivered-To: ************@gmail.comReceived: by 10.70.102.67 with SMTP id fm3csp1378385pdb;        Mon, 27 Apr 2015 09:20:55 -0700 (PDT
)X-Received: by 10.68.217.106 with SMTP id ox10mr23174020pbc.21.1430151654873;        Mon, 27 Apr 2015 09:20:54 -0700 (PDT)Return-Path: <b
05524c6220********[email protected]>Received: from spruce-goose-ab.twitter.com (spruce-goose-ab.twitter.com. [199.59.150
.71])        by mx.google.com with ESMTPS id 6si30521501pds.59.2015.04.27.09.20.54        for <**********@gmail.com>

But well here's my problem, this thing I got is not really what I want. I want to know if there is a way to see it clearly, just like a real mail box but in my terminal.

Here's the code by the way :

import getpass, poplib

user = '**********@gmail.com'
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
Mailbox.user(user)
Mailbox.pass_('*********')
numMessages = 1 #len(Mailbox.list()[1]) #Only one mail                
file = open("mail.html", "w")
for i in range(numMessages):
    for msg in Mailbox.retr(i+1)[1]:
        file.write(msg)
file.close
Mailbox.quit()

Upvotes: 1

Views: 3400

Answers (1)

meuh
meuh

Reputation: 12255

The result of retr() is a tuple (response, ['line', ...], octets) of which you are keeping the list of lines. In the example given at the end of python doc they show

for j in M.retr(i+1)[1]:
    print j   

which you have converted to

for msg in Mailbox.retr(i+1)[1]:
    file.write(msg)

The difference is that print adds a newline, and your write does not. Just add a "\n" after every write().

However, I agree that you only seem to have the headers...

Upvotes: 2

Related Questions