How to save a email message?

TheSaint nobody at nowhere.net.no
Sat Jul 2 13:42:32 CEST 2011


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
>>> from mailbox import mbox as Mbx
>>> mbx= Mbx('/tmp/mb', True)
>>> mbx.add(fp)
0
>>> mbx.get_message(0)
<mailbox.mboxMessage object at 0x1704d10>
>>> print(mbx.get_message(0))


>>>
===================================================================

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

-- 
goto /dev/null



More information about the Python-list mailing list