help with rfc822 please :)

Tim Roberts timr at probo.com
Sat Mar 4 17:59:55 EST 2000


Matthew Barre <mbarre at mac.com> wrote:

>Can someone help me with the rfc822 module, I read its section in the
>library reference, but I'm still having trouble with it. Why doesn't the
>following work, and could someone give a small example that is similar and
>does work?
>
>import poplib, rfc822
>
>mserver = poplib.POP3('mail.server.com')
>mserver.getweclome()
>mserver.user('myname')
>mserver.pass_('pass')
>nmsg = open('inbox','w')
>nmsg = mserver.retr(1)    #retrieve a message and assign it to a variable
>rmsg = rfc822.Message(nmsg) #create new message instance using rfc module
>print rmsg.getaddr('From') #attempt to retrieve the from

You open a file, assign a reference to nmsg, then promptly overwrite the
variable with the message, thereby closing the file.

If you want to use a file for this, you need to write the message to the
file, rewind it, and pass the file variable to rfc822.Message:

nmsg = open('inbox','w')
for ln in mserver.retr(1)[1]:
    nmsg.write( ln + "\n" )
nmsg.seek(0)
rmsg = rfc822.Message( nmsg )

If you'd rather not use a file, you can use StringIO to do the same thing.

sio = StringIO()
sio.write( join( mserver.retr(1)[1], '\n' ) )
sio.seek(0)
rmsg = rfc822.Message( sio )
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list