[Tutor] email & imaplib for beginners like me

Liam Clarke cyresse at gmail.com
Sat Oct 16 13:38:48 CEST 2004


Hello again,

One last niggle. I've managed to download a MIME multi-part, and
successfully decode an attachment, so thanks for all your help.

I'm just having trouble trying to get a specific header field : -

I can't find any examples, but this is what I'm trying - 

(a, b)=session.fetch(5, '(BODY[HEADER.FIELDS[FROM]])'' )

and variants thereof (of all 4 bracket sets), all involving FROM being
in brackets after HEADER.FIELDS, as I get this error :
['Missing required open parenthesis in Fetch BODY[HEADER.FIELDS']

Normal error is this
FETCH command error: BAD ['Invalid body section']

So yeah, getting mildly frustrated with precise syntaxes (syntaxii?),
RFC 2822 and 3501 aren't hugely helpful on precise locations of
brackets.

>From RFC 3501:

in BODY
HEADER.FIELDS and HEADER.FIELDS.NOT are  followed by a list of
field-name (as defined in [RFC-2822]) names, and return a subset of
the header. ...

Thanks in advance for your time.

Liam Clarke


On Sat, 16 Oct 2004 11:31:20 +1300, Riumu Kuraku <cyresse at gmail.com> wrote:
> On Sat, 16 Oct 2004 11:28:41 +1300, Riumu Kuraku <cyresse at gmail.com> wrote:
> 
> 
> > With...
> > x=session.fetch(1, "(RFC822)")
> >
> > >>>print x[0]
> > OK
> > >>>print x[1]
> > (All of x except for the 'OK')
> >
> > >>>print x[1][0] is the same output as above
> >
> > but, >>>print x[1][0][1] gives the full email, relatively laid out....
> > print x[1][0][0] gives
> > 1 (RFC822 {12273}
> >
> > >>> print x[1][1] gives me this:
> > )
> > >>> print x[2] give list index out of range.
> >
> > So, to clarify my understanding, when I fetch,
> > I get a tuple of 2 values, x[0], and x[1]. x[0] is the server return
> > code (Ok, BAD etc.)
> > and x[1] is the data I fetched, plus an additional code.
> >
> > x[1][0]is the full data I feteched + that additional code , x[1][1] is
> > a closed bracket? Is this normal?
> >
> > Then, x[1][0] is divided into two bits,x[1][0][0] which is the code
> > which is 1 (RFC822){12273}, which is (guessing here) UID, what I
> > requested and... ? then,
> > x[1][0][1] is the data without server return codes, or that additional code.
> >
> > How am I doing?
> >
> > So, for error catching, I check x[0], not sure what I'd use x[1][0][0]
> > for, if it is the UID, it would be good for keeping track of which
> > email is where, and x[1][0][1] is what I was trying to fetch.
> >
> > Thanks, Michael, and please correct me if my conclusions are erroneous.
> > /me runs off to re-examine tuples in tutorial.
> >
> > (Oh, and those links you posted don't point to live sites, but google
> > has caches -)
> > http://www.google.co.nz/search?q=cache:YWgEVsgYXgAJ:aspn.activestate.com/ASPN/Mail/Message/python-Tutor/1619598+&hl=en
> > http://www.google.co.nz/search?q=cache:4Fr-5SCPsTgJ:aspn.activestate.com/ASPN/Mail/Message/python-Tutor/1619609+&hl=en
> >
> > Regards,
> >
> > Liam Clarke
> >
> >
> > On Fri, 15 Oct 2004 18:38:36 +0200, Michael Janssen
> > <mi.janssen at gmail.com> wrote:
> > > On Thu, 14 Oct 2004 21:42:59 +1300, Riumu Kuraku <cyresse at gmail.com> wrote:
> > >
> > > > Just tryng to understand the email package, and the docs are a
> > > > little... sparse?
> > >
> > > Would be nice to have some more examples in the example section.
> > > Someone needs to write them ...
> > >
> > > > a=test.fetch(1, 'RFC822' )... The first argument of fetch is fine.
> > > > It's the second that's got me. It's a name of a Internet standard. And
> > > > I used it on three different emails, and it got two headers, and one
> > > > header and full text. So I am quite confused as to how exactly to get
> > > > specific things, like headers/text/attachments only, so any light that
> > > > can be shed in that regard would be fantastic.
> > >
> > > some time past, but googles knows it:
> > > http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/1619598
> > >
> > > and:
> > > http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/1619609
> > >
> > > To quote myself:
> > > "rfc3501 section 6.4.4 lists all possibilities"
> > >
> > > --> section 6.4.5 lists all possibilities of the fetch command.
> > >
> > > That's the point the the imaplib docs: you *need* to read the rfc's to
> > > get them (imaplib docs should state this ...).
> > >
> > > > >>>print j.parse(x)
> > > > AttributeError: 'tuple' object has no attribute 'readline'
> > > >
> > > > #OK, so x is a tuple, and it has no attr, readline...hmmm...
> > >
> > > it's a tuple of the server returncode ("OK") and a list of messages
> > > (which are tuples in turn).
> > >
> > > x[1] # list of messages
> > > x[1][0] # first one
> > > x[1][0][1] # content of first one. That's bad, eh?
> > >
> > > > #Run off to docs, and open parser.py to have a look and:
> > > >
> > > > >>>print j.parsestr(x)   #Turns out, Parser.parse() is for file
> > > > objects... Parser.parsestr()
> > > >                                   # is for string objects, as I am about to learn.
> > > >
> > > > TypeError: expected read buffer, tuple found
> > > >
> > > > #Still not liking tuple. Hmmm...
> > >
> > > > >>>i=str(x)
> > > > >>>print j.parsestr(i)
> > >
> > > converting the whole tuple into a string, poor parser is confused by
> > > all those brackets and 'OK' messages. So it will "parse" message
> > > without extracting much information from it.
> > >
> > > try something like:
> > >
> > > msg =  x[1][0][1]
> > > print msg
> > >
> > > or:
> > > print j.parsestr(msg)
> > >
> > > regards
> > > Michael
> > >
> >
>


More information about the Tutor mailing list