Skip to content

Instantly share code, notes, and snippets.

@consatan
Forked from akpoff/curl_imap_query_commands.md
Last active April 18, 2024 22:08
Show Gist options
  • Save consatan/69bcceb3cb8a5ef372049cc609a5d609 to your computer and use it in GitHub Desktop.
Save consatan/69bcceb3cb8a5ef372049cc609a5d609 to your computer and use it in GitHub Desktop.
curl commands to query imap servers

curl commands to query imap servers

Based on https://busylog.net/telnet-imap-commands-note/

curl options

  • -k -- don't verify certificate (optional)
  • -n -- use .netrc for username and password (optional)
  • -X -- request to send to server
curl -n imaps://mail.server.tld -X 'imap command'

.netrc format

machine mail.server.tld login username password supersecretpw

machine mail.server.tld
    login username
    password supersecretpw

url format

  • tls -- imaps://
  • plain -- imap://

Note: enclose url in quotes if your user id or password have shell characters

Examples

get count of message in mailbox INBOX

curl -n imaps://mail.server.tld -X 'STATUS INBOX (MESSAGES)'
* STATUS INBOX (MESSAGES 36)

get count of unseen (unread) message in mailbox INBOX

curl -n imaps://mail.server.tld -X 'STATUS INBOX (UNSEEN)' 
* STATUS INBOX (UNSEEN 20)

get count of unseen (unread) message in mailbox sub-mailbox

curl -n imaps://mail.server.tld -X 'STATUS Archive.2016 (MESSAGES)' 
* STATUS Archive.2016 (MESSAGES 3069)

get count of recent messages in mailbox INBOX (received but not listed)

curl -n imaps://mail.server.tld -X 'STATUS INBOX (RECENT)'   
* STATUS INBOX (RECENT 0)

list all mailboxes/folders

curl -n imaps://mail.server.tld -X 'LIST "" "*"'                     
* LIST (\HasNoChildren \Drafts) "." Drafts
* LIST (\HasChildren) "." Archive
* LIST (\HasNoChildren) "." Archive.2013
* LIST (\HasNoChildren) "." Archive.2014
* LIST (\HasNoChildren) "." Archive.2015
* LIST (\HasNoChildren) "." Archive.2016
* LIST (\HasChildren) "." Archive.old_account
* LIST (\HasNoChildren) "." Archive.old_account.sent
* LIST (\HasNoChildren \Junk) "." Junk
* LIST (\HasNoChildren \Sent) "." "Sent Messages"
* LIST (\HasNoChildren \Trash) "." Trash
* LIST (\HasNoChildren) "." INBOX

fetch mail

curl -n 'imaps://mail.server.tld/INBOX;UID=1'
# fetch gmail Drafts mailbox
curl -n 'imaps://imap.gmail.com:993/%5BGmail%5D/Drafts;UID=1'
# fetch gmail Sent Mail mailbox
curl -n 'imaps://imap.gmail.com:993/%5BGmail%5D/Sent%20Mail;UID=1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment