imalib cant get From: adresses

Barry Scott barry at barrys-emacs.org
Wed Jan 27 16:29:50 EST 2021


Sigh you use a damaged email address for reply does not work.

> On 27 Jan 2021, at 14:30, Bischoop <Bischoop at vimart.net> wrote:
> 
> 
> I don't have much experience with imaplib and for a few days can't
> manage to get properly, I followed also some tutorial but also met few
> problems.
> What I want is to get email addresses from emails I do have in gmail
> inbox. The code below gets them but then pops an error.
> When I change the part "for i in range()" as in commented line it gets
> just the last email.
> 
> Anybody familiar here with imaplib? 

You have assumed that message ID are integers and in order.
Where as they are assigned by the IMAP server and are not in order.
I'm not even sure that they need to be integers.

You need to search the for the messages you want to get back a list of msg_id's.

The you can use the loop over the msg id doing operations on them.

After you M.select() call M.search() with appropriate params.
search is very powerful and will get you only the messages you are interested in.

Here is a fragment of some of my code to show the idea:

       folder_info = self.server.select_folder( folder, readonly=True )
       msg_ids = self.server.search( ['ALL'] )
       print( msg_ids )

       all_msg_info = self.server.fetch( msg_ids, ['FLAGS','ENVELOPE'] )
       for msg_id in all_msg_info:
           msg_info = all_msg_info[ msg_id ]
           print( '%-40s %s %s' % (msg_info[b'FLAGS'], msg_info[b'ENVELOPE'].date, msg_info[b'ENVELOPE'].subject) )

Barry

> 
> 
> import getpass, imaplib
> import email
> from email.header import decode_header
> 
> M = imaplib.IMAP4_SSL('imap.gmail.com')
> M.login(u, p)
> M.select()
> status, messages = M.select("INBOX")
> di={}
> 
> # number of top emails to fetch
> N = 100
> 
> # total number of emails
> messages = int(messages[0])
> 
> for i in range(messages, messages-N,-1):
> #for i in range(messages, N):
>   # fetch the email message by ID
>   res, msg = M.fetch(str(i), "(RFC822)")
>   for response in msg:
>       if isinstance(response, tuple):
>           # parse a bytes email into a message object
>           msg = email.message_from_bytes(response[1])
> 
> 
>           From, encoding = decode_header(msg.get("From"))[0]
>           if isinstance(From, bytes):
>               From = From.decode(encoding)
>           print("From:", From)
>           print(type(From))
> 
> 
>           print("="*100)
>           if '<'in From:
>               s = From.index('<')
>               d = From.index('>')
>               q = From[s + 1:d]
>               w = From[0:s - 1]
>               if q in di.values():
>                   pass
>               else:
>                   di[w] = q
>           else:
>               if q not in di.values():
> 
>                   b = sum(1 for key in di if key.startswith('Friend'))
>                   di[f'Friend{b+1}']=From
> # close the connection and logout
> M.close()
> M.logout()
> 
> print(di)
> for i in di.keys():
>   if 'Friend' in i:
>       print(From,'\n',i,di[i])
> 
> b = sum(1 for key in di if key.startswith('Friend'))
> print(b)
> 
>   raise self.error('%s command error: %s %s' % (name, typ, data))
> imaplib.error: FETCH command error: BAD [b'Could not parse command']
> 
> Process finished with exit code 1
> 
> 
> 
> ========================
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list