How to save a email message?

T

TheSaint

Hello,
I'm trying to gather some mail and save it. I don't get why it isn't saved
as expected.

==========================================================================
import poplib, socket, sys
from configparser import Error as ProtocolError
args= sys.argv[1:] # this is fake but is here as example
def Pop3_access(*args):
'''Accessing a pop server, doing a function and return a result'''
func= args[0]; args= args[1:]
try:
pop= poplib.POP3(srv_info[0])
pop.user(srv_info[1])
pop.pass_(srv_info[2])
except (poplib.error_proto):
raise ProtocolError
except (socket.timeout,socket.gaierror):
try: pop.quit()
except NameError: pass # it wasn't started
return
result= func(pop, args)
pop.quit()
return result
def func(pop, N):
.... return pop.retr(N)
....
msg= Pop3_access(func, 4)
from io import BytesIO as B
fp= B()
for l in msg[1]:
.... fp.write(l)
.... fp.write('\n'.encode())
....
34
1
50
1
63
1
65
1
36
1
52
1
41
1
114
1
45
1
38
1
74
1
56
1
37
1
34
1
28
1
23
1
33
1
56
1
57
1
17
1
44
1
31
1
54
1
30
1
30
1
0
1
31
1
0
1
39
1
0
1
12
1
32
1
49
1
0
1
6
1
64
1
68
1
0
1===================================================================

The result is an empty message, but the fp.getvalue() shows file-like
stream.
Am I missing some point?
 
S

Steven D'Aprano

TheSaint said:
Hello,
I'm trying to gather some mail and save it. I don't get why it isn't saved
as expected.

Short answer: you need to seek the fp file object back to the start of the
file by calling fp.seek(0). Otherwise, when you add it to the mail box, if
gets EOF immediately and nothing gets added.

More comments below:

==========================================================================
import poplib, socket, sys
from configparser import Error as ProtocolError
args= sys.argv[1:] # this is fake but is here as example


It makes it very hard for us to run your code when you paste the interactive
session. For small snippets, it's okay to just post the interactive
session, but for more substantial code, it is best to past the function
definition ready for us to copy and paste:

def foo():
pass

def Pop3_access(*args):
'''Accessing a pop server, doing a function and return a result'''
func= args[0]; args= args[1:]

Why don't you declare func as a named argument?

def Pop3_access(func, *args):

try:
pop= poplib.POP3(srv_info[0])
pop.user(srv_info[1])
pop.pass_(srv_info[2])
except (poplib.error_proto):
raise ProtocolError
except (socket.timeout,socket.gaierror):
try: pop.quit()
except NameError: pass # it wasn't started
return
result= func(pop, args)
pop.quit()
return result
def func(pop, N):
... return pop.retr(N)
...
msg= Pop3_access(func, 4)
from io import BytesIO as B
fp= B()
for l in msg[1]:
... fp.write(l)
... fp.write('\n'.encode())
...
34
1
50
1
[...]


For those wondering, as I was, what all these numbers are, the
BytesIO.write() method returns the number of bytes written.

At this point, fp is a file-like object containing a number of bytes, but
the file pointer is at the end of the file, so when you read from it, it
immediately gets EOF (i.e. empty).
b''

But if you seek back to the beginning:

*raises eyebrow*

Do you need to rename mbox? You don't even save a keypress:
shift-m b x and m b o x both take 4 keypresses.
 
T

TheSaint

Steven D'Aprano wrote:

Thank you very much.
But if you seek back to the beginning:

b'hello'

Found the matter and *is* working
I discover another problem:
one message contains also a different encoding, but mostly it is not
possible to represent that writing on the normale console output.
raises eyebrow

Do you need to rename mbox? You don't even save a keypress:
shift-m b x and m b o x both take 4 keypresses.

You stroke a right point :D
I could just use its own name

The code was not exactly run from the python shell. I was trying to make a
*complete* picture from my module. In fact there aren't the arguments to
access the pop server.
I'd like to mention that I've looked into the output file, which shows only
"From MAILER DEAMON" + date of the action, but no message at all.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top