Test IMAP with curl (IMAP example)

Notes how to perform  IMAP test with curl:
 $curl imap://username:password@in.example.com/INBOX?NEW

Scheme

scheme://host:port/path

Schemes for the e-mail protocols (supported by curl) are :
imap://user:password;options@in.example.com
pop3://user:password;options@in.example.com
smtp://user:password;options@in.example.com

More information :
https://tools.ietf.org/html/draft-earhart-url-smtp-00 (SMTP URL scheme – IETF draft)
https://tools.ietf.org/html/rfc5092 (IMAP URL scheme )
https://github.com/curl/

Starting notes

In order to show commands (i.e. IMAP commands) you can user option -v
curl -v imap://user:password@in.server.com/

curl imap://user:password@ in.example.com 
For an e-mail address such as <user>@gmail.com the server to use is not  gmail.com but is imap.gmail.com (IMAP server).


In case you need to use “special” chars like @ you have to escape in according to  RFC 3986
Example (password: p@ssword) @ is escaped using %40
curl “imap://username:p%40ssword@in.example.com”
IMAP implicit SSL with:
curl “imaps://user:password@in.example.com”
Send direct command:
curl “imap://username:password@in.example.com” -X ‘<COMMAND>’

-X, –request   (HTTP)  Specifies a custom request method to use when communicating with the HTTP server.
The specified request method will be used instead of the method otherwise used (which defaults to GET).  Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests include PUT and DELETE,  but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.  …  https://curl.haxx.se/docs/manpage.html

Example :
$ curl “imap://username:password@in.example.com” -X ‘LIST “” “*”
$ curl “imap://username:password@in.example.com” -X ‘CAPABILITY
$ curl “imap://username:password@in.example.com” -X ‘
EXAMINE INBOX


A easy way in order to specify commands
CIP=”<COMMAND>”;curl “imap://username:password@in.example.com/” -X “$CIP”

Example :
CIP=”LIST \”\” \”*\””;curl “imap://username:password@in.example.com/” -X “$CIP”
CIP=”CAPABILITY“;curl “imap://username:password@in.example.com/” -X “$CIP”


if you get curl: (1) Protocol imap not supported or disabled in libcurl
curl -V
curl 7.51.0 (x86_64-apple-darwin16.0) libcurl/7.51.0 SecureTransport zlib/1.2.8
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp

Test IMAP with curl

Authentication

curl “imap://username:password@in.example.com”
…or…
curl “imap://in.example.com/INBOX” –user “username:password” 

Capability

curl “imap://username:password@in.example.com” -X  ‘CAPABILITY’

curl imap://username:password@in.example.com -X  'CAPABILITY'
* CAPABILITY IMAP4rev1 IDLE LOGIN-REFERRALS NAMESPACE QUOTA CHILDREN  MOVE AUTH=CRAM-MD5 AUTH=DIGEST-MD5 AUTH=PLAIN

Top level list of folders

curl “imap://user:password@in.example.com” 

curl "imap://username:password@in.example.com"
* LIST (\HasNoChildren) "/" Draft
* LIST (\HasChildren) "/" "Deleted Messages"
* LIST (\HasNoChildren) "/" Drafts
* LIST (\HasChildren) "/" INBOX
* LIST (\HasNoChildren) "/" INBOX/test
* LIST (\HasNoChildren) "/" INBOX/test2
* LIST (\HasNoChildren) "/" Trash

Check new email INBOX

curl “imap://username:password@in.example.com/INBOX?NEW”

$curl "imap://username:password@in.example.com/INBOX?NEW"
* SEARCH 213

Fetch the new email (or fetch email specifying UID) 

Warning to fetch big email…

curl “imap://username:password@in.example.com/<FOLDER>;UID=<UID_NUMBER>”

curl "imap://username:password@in.example.com/INBOX;UID=213"
Return-Path: <testme@sender.me>
Received: from smtpXY.example.com (192.168.1.2) by ...
...
From: Jhon <testme@sender.me>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0 (Mac OS X Mail 10.2 \(3259\))
Subject: Test New
Message-Id: <000000-000000A8C524021DD4@example.com>
Date: Thu, 2 Mar 2017 11:50:19 +0100
To: username@example.com
X-Mailer: Apple Mail (2.3259)

Test body
 

Fetch BODY (without header)

Warning to fetch big email…

curl “imap://username:password@in.example.com/<FOLDER>;UID=<UID_NUMBER>/;SECTION=TEXT”

curl "imap://username:password@in.example.com/INBOX;UID=213/;SECTION=TEXT"
Test body

Fetch partially

curl “imap://username:password@in.example.com/<FOLDER>;UID=<UID_NUMBER>/;PARTIAL=<FROM>.<TO>”

curl "imap://username:password@in.example.com/INBOX;UID=214/;PARTIAL=0.255"
Return-Path: <from@example.com>
Received: from smtpXYZ.xyz.it (1.1.1.1) by smtp.xyz
 id 58BASF2035193 for testme@xyz.it; Thu, 2 Mar 2017 12:23:38 +0100
... to 255 chars

...or...
curl "imap://username:password@in.example.com/INBOX;UID=214/;SECTION=TEXT/;PARTIAL=0.255"
--Apple-Mail=_A46E4CA4-B32B-4936-A24D-9795751A92B2
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=utf-8
BODY PART
... to 255 chars

curl "imap://username:password@in.example.com/INBOX;UID=213/;SECTION=1"
is the command IMAP :
. FETCH 213 BODY[1]

$curl "imap://username:password@in.example.com/INBOX;UID=213/;SECTION=2.3"
is the command IMAP :
. FETCH 213 BODY[2.3]

SEARCH

curl “imap://username:password@in.example.com/INBOX?<QUERY>”
curl "imap://username:password@in.example.com/INBOX?SUBJECT%20test"
* SEARCH 1 2 4 5 6 83 184 186 210 211 213 214

curl "imap://username:password@in.example.com/INBOX?SINCE 18-apr-2016"
* SEARCH 1 2 4 5 6 12 23 210 211 213 214

curl "imap://username:password@in.example.com/INBOX?BEFORE%2018-apr-2016"
* SEARCH 1  44 45 46 47 48 49 50 51 53 194 195

curl "imap://username:password@in.example.com/INBOX?BEFORE%2012-apr-2016%20SINCE%2015-apr-2015"
* SEARCH 148 149 150 151 151 192 193 194 195

Append

curl “imap://username:password@in.example.com/<FOLDER>” -T <FILE>

Create <FILE>

Content file doesn’t matter.
vi /tmp/email2append.txt
Return-Path: <giovanni@busylog.net>
From: Giovanni <giovanni@busylog.net>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0 (Mac OS X Mail 10.2 \(3259\))
Subject: Test me APPEND
Date: Thu, 4 Mar 2017 11:50:19 +0100
Cc: Giovanni <giovanni@busylog.net>
To: You <you@example.com>

Test

Append  <FILE>

curl "imap://username:password@in.example.com/INBOX" -T /tmp/email2append.txt
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100   327    0     0  100   327      0    288  0:00:01  0:00:01 --:--:--   288

...or (folder/subfolder)...

curl "imap://username:password@in.example.com/test/test2" -T /tmp/email2append.txt
curl imap append
curl imap append

6 Comments

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.