help with rfc822 please :)

Matthew Barre mbarre at mac.com
Sun Mar 5 04:28:42 EST 2000


Thank you. I'm a newbie and sometimes miss the simple stuff.

-Matt
---------
"Ray! When someone asks you if you're a god, you say YES!!" -Winston Zedmore

> From: Tim Roberts <timr at probo.com>
> Organization: Providenza & Boekelheide, Inc.
> Newsgroups: comp.lang.python
> Date: Sat, 04 Mar 2000 22:59:55 GMT
> Subject: Re: help with rfc822 please :)
> 
> 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