Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to print inbox messages one by one (like cat someFile) so that I can parse the message contents.

something like printGmail InboxMesg1 | grep some | sed some

Using curl I can access the first inbox message curl -u '*myusername:mypassword*' -X "SEARCH UNSEEN" imaps://imap.gmail.com:993/INBOX

For example I can tell my self to execute a command by email curl -u '*myusername:mypassword*' -X "SEARCH UNSEEN" imaps://imap.gmail.com:993/INBOX grep someCommand && echo request received executing command

How I can print the last (newest), 2nd last, etc messages. Also how to read emails in other folders such as spam, sent, etc

share|improve this question
Use the API (with a real parser), or a real IMAP library. Really. – Chris Down Jan 24 '14 at 9:36
@Chris Down how to use real parser ?. – totti Jan 24 '14 at 14:20

This is an odd question without providing more context ... There is a tool called OfflineIMAP which will sync a local Maildir hierarchy with a server. If you use this without any filters it will create a file for each email in your inbox on your computer, which you then can arbitrarily process ... it can even sync a changed email back to your inbox then.

If you want to use IMAP yourself, you need to check curl's documentation first, because according to the IMAP RFC

The untagged SEARCH response from the server
contains a listing of message sequence numbers corresponding to
those messages that match the searching criteria.

So you it should return the sequence numbers of all unseen emails. Also according to the RFC, the message sequence numbers must be ordered ascending with the message uid which must be ascending with the order in which emails enter your mailbox. So in theory, when you send a SEARCH UNSEEN you would receive an list of ids that can simply be sorted to the order in which emails entered your inbox. You can then FETCH the individual emails by exactly those ids.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.