imap folder scanner

Sebastjan Trepca trepca at gmail.com
Sat Mar 25 18:40:01 CET 2006


"senders" is list, that is why that regex does not work. I don't like
regexes that much so you can try this:

parsed_senders = []
sender = ""
for item in senders:
   if isinstance(item,tuple):
      item= ''.join(item)
   if item==')':
      parsed_senders.append(sender[sender.find('From:')+5:].strip())
      sender = ""
   else:
      sender+=item
print parsed_senders

Sebastjan

On 3/25/06, Kun <neurogasm at gmail.com> wrote:
>
> Marco Carvalho wrote:
> > On 3/24/06, Sebastjan Trepca <trepca at gmail.com> wrote:
> >
> >> m.select('myfolder')
> >
> > Some attention is required here to retrieve subfolders.
> > Some imap servers like Cyrus and Courier uses "INBOX.subfolder" to
> > access subfolders.
> > --
> > Marco Carvalho (macs) | marcoacarvalho(a)gmail.com
> > http://arrakis.no-ip.info  | http://cdd.debian-br.org
> > Maceio - Alagoas - Brazil
> > Debian GNU/Linux unstable (Sid)
> > GNU-PG ID:08D82127 - Linux Registered User #141545
> > Notícias Semanais do Debian em Português:
> http://www.debian.org/News/weekly
> > Alertas de Segurança Debian (DSA): http://www.debian.org/security
>
>
> so i have used the following code and have successfully saved a list of
> senders as a string.  however, the string has much more information than
> just the email address and i am wondering what is the best way to parse
> the email address out of the entire string.
>
> sample string:
> >>> print status, senders
> OK [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
> <anon at anon.com >\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
> 'From: Kun <neurogasm at gmail.com>\r\n\r\n'), ')']
>
> how do i just get the two email addresses out of there?
>
> my code is:
>
> from imaplib import *
> import getpass
> m = IMAP4("xxxxxxxx")
> m.login('xxxxxx', 'xxxxxxx')
> m.select('Inbox')
> status, data = m.search(None,'(SUBJECT "BIKES")')
> assert status=='OK', "Error. Message: %s"%data
> data = data[0] #you get your results in a list and search returns only
> one result
> assert data,"No results"
> #cool, we have results, but IMAP's search command only returns IDs so we
> have to fetch
> #msgs now
> status,senders = m.fetch(data.replace(' ',','),'(BODY.PEEK[HEADER.FIELDS
> (FROM)])')
> assert status=='OK', "Error. Message: %s"%data
> print senders
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Sebastjan
http://www.trepca.si/blog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060325/75676883/attachment.html>


More information about the Python-list mailing list