0

Dear users of Stack Overflow,

My question is about how to use the returned data of an imap command in python (in this case printing the amount of messages in your inbox). What I was able to find on the internet is limited to the following two descriptions:

Discription 1

Discription 2

Reading these explanations, I still have no idea how to use the EXISTS response as I’ve just started programming (one semester of C programming at Uni). So, if someone could help me understand how the responses of imap commands can be used in python, that would be awesome. I do prefer to understand the principle of the responses instead of solving just this one-time issue so I’ll be able to use responses in different situations (and other people might be able to apply it too then).

The (basic) code I’ve written so far on my Raspberry Pi including the point where I'm stuck with EXISTS (between the two question marks):

import imaplib
server = imaplib.IMAP4_SSL(‘imap.gmail.com’)
server.login(‘USERNAME’, ‘PASSWORD’)
server.list()
server.select(‘inbox’)

print ‘%d messages in the inbox’ %d ??EXISTS??

Hopefully I’m not the only one who would like to know this!

Kind regards,

I. Van Dijck

P.S. My updated code is as follow (the error is: TypeError: not all arguments converted during string formatting):

import imaplib
server = imaplib.IMAP4_SSL('imap.gmail.com')
server.login('USERNAME', 'PASSWORD')
server.list()
server.select('inbox')
number_of_messages = server.select("inbox")

print '%s messages' % number_of_messages
  • The `select` function returns the number from the exists response. `number_of_messages = server.select("inbox")` – Max Sep 23 '16 at 15:23
  • Hi Max, thanks for replying! Your answer makes sort of sense to me :) However, I still have problems printing this number, as I get an error. And what I still don't understand is, why is there an EXISTS? I assume this has a purpose, but I can't seem to get this from your answer. So where do I use this EXISTS? This is what I have added to the code (starting from 'server.select('inbox')', see my initial question): number_of_messages = server.select("inbox") print "%d messages in the inbox" %d number_of_messages The error is: %d format: a number is required, not str – I. Van Dijck Sep 26 '16 at 11:00
  • `number of messages = server.select("inbox") print "%d messages in the box" %d number_of_messages` (I couldn't edit my comment anymore, but here is the code in grey. Trying to make things more convenient haha) – I. Van Dijck Sep 26 '16 at 11:08
  • `print '%d messages' %d number_of_messages` isn't valid code. Try `print '%d messages' % number_of_messages`. – Max Sep 26 '16 at 13:24
  • Thanks Max, but still have an error. I've put down my complete code, and the error is: TypeError: %d format: a number is required, not str. Does this mean I have to initialise number_of_messages as integer (because it's a string now, as the return of EXISTS response is a string?)? `import imaplib server = imaplib.IMAP4_SSL('imap.gmail.com') server.login('USERNAME', 'PASSWORD') server.list() server.select('inbox') number_of_messages = server.select("inbox") print '%d messages' % number_of_messages` I hope you could help me out – I. Van Dijck Sep 26 '16 at 15:12
  • Maybe. Try %s to print it out. %s will always work. – Max Sep 26 '16 at 15:45
  • Hi Max, I tried that one already. But when I do `print '%s messages' % number_of_messages` it gives me the following error: TypeError: not all arguments converted during string formatting. Do you know what this is? – I. Van Dijck Sep 26 '16 at 17:02
  • Please add your new code to the question. Your comments and your error message don't quite match. – Max Sep 26 '16 at 18:41
  • I've added my complete code to the bottom of my question (in the P.S. section) – I. Van Dijck Sep 26 '16 at 19:24
  • Oh, select returns a tuple. Do typ, dat = inbox.select(...), then just print dat and see what you get. – Max Sep 26 '16 at 19:26
  • I've done what you've said, and it works. The output is: ['3'] messages However, I don't understand what I've done with the 'typ'. So could you explain 'typ'? And I believe that 'dat' could have been 'number_of_messages', right? Thanks for your help btw! Is there also a way to just print out: 3 messages ? So without those weird brackets? – I. Van Dijck Sep 26 '16 at 19:54
  • Most imaplib functions return an error code, and the data in typ, dat. `typ` will be "OK", "NO", or "BAD" (usually). If it is "OK", the command succeeded. `dat` then contains the response. In this case, you probably want `number_of_messages = int(dat[0])`. That is, get the the first element of the returned list, and change it to an int. – Max Sep 27 '16 at 14:03

1 Answers1

0

The select function returns the number from the EXISTs response. Almost all imaplib commands return two values, type, and data.

From the documentation

Each command returns a tuple: (type, [data, ...]) where type is usually 'OK' or 'NO', and data is either the text from the command response, or mandated results from the command. Each data is either a string, or a tuple. If a tuple, then the first part is the header of the response, and the second part contains the data (ie: ‘literal’ value).

select, in it's list of data returns the number from the EXISTS response, apparently as a string. You will need to extract the item from the list, and convert it to a string:

typ, dat = inbox.select()
number_of_messages = int(dat[0])
Max
  • 10,701
  • 2
  • 24
  • 48
  • This has worked for me. Thanks for the explanation, I think I understand it better (I've ordered some books btw as well, so I can study Python :) ). Thank you again, Max, for your time and effort! – I. Van Dijck Sep 27 '16 at 17:37