libcurl and imap
Asked Answered
J

2

10

I noticed that libcurl supports IMAP, but didn't find anything in the documentation and example http://curl.haxx.se/libcurl/c/imap.html is very poor. Does anyone know how to use this protocol in this lib, how to download mails? Regards

Joannejoannes answered 22/4, 2012 at 11:20 Comment(1)
The IMAP parts lack specific documentation, feel free to join us in the curl project and help us improve this.Ganof
P
13

There is a good email on the mailing list describing the IMAP syntax here: http://curl.haxx.se/mail/lib-2013-03/0104.html

This is the relevant part:

--url imap://mail.example.com/INBOX/;UID=1 - Selects the user's inbox and fetches message 1

--url imap://mail.example.com/INBOX;UIDVALIDITY=50/;UID=2 - Selects the user's inbox, checks the UIDVALIDITY of the mailbox is 50 and fetches message 2 if it is

--url imap://mail.example.com/INBOX/;UID=3/;SECTION=TEXT - Selects the user's inbox and fetches message 3 with only the text portion of the message

In addition to retrieving an email it is also possible to transfer an email to the server. This is achieved through IMAPs APPEND command and simply utilising curl's existing mechanism for uploading data to a server:

--url imap://mail.example.com/OUTBOX -T email.txt

  • Other commands can be issued, such as: LIST to list the sub-folders of a mailbox; EXAMINE to see what emails exist in a mailbox; CREATE, RENAME and DELETE to manipulate sub-folders, STORE to change the properties of an email and others. Some examples are as follows:

--url imap://mail.example.com - Performs a top level folder list

--url imap://mail.example.com/INBOX - Performs a folder list on the user's inbox

--url imap://mail.example.com -X "EXAMINE INBOX" - Performs a message list on the user's inbox

--url imap:// mail.example.com/INBOX -X "CREATE Sub-folder" - Creates a sub-folder within the user's inbox

--url imap:// mail.example.com/INBOX -X "RENAME Sub-folder My-folder" - Renames the sub-folder within the user's inbox to my-folder

--url imap:// mail.example.com/INBOX -X "DELETE Sub-folder" - Deletes the sub-folder within the user's inbox

--url imap:// mail.example.com/INBOX -X "STORE 1 +Flags \Deleted" - Deletes message 1 from the user's inbox

--url imap:// mail.example.com/INBOX -X "STORE 1 +Flags \Seen" - Marks message 1 in the user's inbox as read

As well as using the URL syntax to fetch a message it is also possible to use the custom request mechanism to perform custom FETCH operations as well. For example

--url imap:// mail.example.com/INBOX -X "'FETCH 1 BODY[TEXT]" will perform the same operation as

--url imap:// mail.example.com/INBOX/;UID=1/;SECTION=TEXT

Photoluminescence answered 4/6, 2013 at 16:10 Comment(0)
O
4

The following command selects my Gmail inbox and issues FETCH 1 BODY[TEXT] to get the first (oldest) message:

curl -v 'imaps://creaktive+gmail.com:[email protected]:993/Inbox'

I'm not quite sure if there is an easy way of performing other tasks, like listing mailboxes or selecting the latest message.

Oink answered 13/11, 2012 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.